我已经在netbeans中创建了新的web-项目(新项目- JavaEE应用程序\##*
它生成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现在我创建了一个新的java类。
@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)
})但也不起作用。
谢谢你帮忙!
发布于 2016-12-23 15:09:49
您的应用程序名称或应用程序上下文根是什么?您需要让您的应用程序上下文根在url中工作,例如域:端口//路由,对于您的情况,它将是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>https://stackoverflow.com/questions/41303722
复制相似问题