找不到路由javaEE
cannot find route javaEE
我在 netbeans 中创建了新的 web- 项目(新项目 - java web - web 应用程序 || 名称:测试 || 服务器:glassFish server 4.1.1 || JavaEE 版本:JavaEE 7 web | | 上下文路径:/测试)
它生成 html 文件,我在其中添加对服务器的简单 ajax 调用。
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
<script>
fetch('/Test/name').then(function(x){
console.log(x)
})
</script>
</body>
</html>
现在(没有脚本标签)当我 运行 这个项目时它把我扔到
localhost:8080/Test
现在我创建一个新的javaclass
@Path("/Test")
@Produces("text/plain")
public class RequestClass {
private String name = "MyName";
private String age = "MyAge";
@GET
@Path("/name")
@Produces("text/plain")
public String getName(){
return this.name;
}
@GET
@Path("/age")
@Produces("text/plain")
public String getAge(){
return this.age;
}
}
现在,当我构建 运行 项目时,服务器响应 404
http://localhost:8080/Test/name 加载资源失败:服务器返回状态 404(未找到)
为什么会这样?路线是对的,为什么找不到?
我正在尝试修复它一段时间,但找不到任何相关信息。
我试着只使用
fetch('/name').then(function(x){
console.log(x)
})
但也没用。
感谢帮助!
您的应用程序名称或应用程序上下文根是什么?您需要让 url 中的应用程序上下文根才能工作,例如 domain:port//routes,对于您的情况,它将是 http://localhost:8080/application-context-root/Test/name
球衣配置
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mkyong.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
我在 netbeans 中创建了新的 web- 项目(新项目 - java web - web 应用程序 || 名称:测试 || 服务器:glassFish server 4.1.1 || JavaEE 版本:JavaEE 7 web | | 上下文路径:/测试)
它生成 html 文件,我在其中添加对服务器的简单 ajax 调用。
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>TODO write content</div>
<script>
fetch('/Test/name').then(function(x){
console.log(x)
})
</script>
</body>
</html>
现在(没有脚本标签)当我 运行 这个项目时它把我扔到
localhost:8080/Test
现在我创建一个新的javaclass
@Path("/Test")
@Produces("text/plain")
public class RequestClass {
private String name = "MyName";
private String age = "MyAge";
@GET
@Path("/name")
@Produces("text/plain")
public String getName(){
return this.name;
}
@GET
@Path("/age")
@Produces("text/plain")
public String getAge(){
return this.age;
}
}
现在,当我构建 运行 项目时,服务器响应 404
http://localhost:8080/Test/name 加载资源失败:服务器返回状态 404(未找到)
为什么会这样?路线是对的,为什么找不到?
我正在尝试修复它一段时间,但找不到任何相关信息。
我试着只使用
fetch('/name').then(function(x){
console.log(x)
})
但也没用。
感谢帮助!
您的应用程序名称或应用程序上下文根是什么?您需要让 url 中的应用程序上下文根才能工作,例如 domain:port//routes,对于您的情况,它将是 http://localhost:8080/application-context-root/Test/name
球衣配置
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.container.servlet.ServletContainer
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.mkyong.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>