【发布时间】:2016-12-23 15:04:35
【问题描述】:
我在 netbeans 中创建了新的 web 项目(新项目 - java web - web 应用程序 || 名称:测试 || 服务器:glassFish 服务器 4.1.1 || JavaEE 版本:JavaEE 7 web || ContextPath:/Test)
它生成 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)
})
但也没有用。
感谢您的帮助!
【问题讨论】:
标签: java jakarta-ee netbeans netbeans-8 java-ee-7