因此,我正在尝试开发具有Java类名称“ Service.java”的Web应用程序,这是我的REST服务。
@Path("/service")
public class Service {
@Context
HttpServletRequest request;
@Context
ServletContext ctx;
@GET
@Path("/getJustAdministrators")
@Produces(MediaType.APPLICATION_JSON)
public Collection<Administrator> getJustAdministrators() {
System.out.println("get administrators");
return getAdministrators().getValues();
}
private Administrators getAdministrators() {
Administrators administrators = (Administrators) ctx
.getAttribute("administrators");
if (administrators == null) {
administrators = new Administrators(ctx.getRealPath(""));
ctx.setAttribute("administrators", administrators);
}
return administrators;
}
链接到我的欢迎页面的main.js文件如下所示:
var rootURL1 = "../eTicket/rest/service/getJustAdministrators";
loadAdministrators();
function loadAdministrators() {
console.log('loadAdministrators');
$.ajax({
type : 'GET',
url : rootURL1,
dataType : "json", // data type of response
success : function(data) {
alert(data);
}
});
}
我在“检查”中遇到这样的错误:
http://localhost:8080/eTicket/rest/service/getJustAdministrators 404(未找到)
我的web.xml文件如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>eTicket</display-name>
<welcome-file-list>
<welcome-file>start.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>eTicket</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
为什么找不到我的资源方法?
答案 0 :(得分:0)
您的子路径可能试图绑定到根,就像/ service一样。
@Path("/getJustAdministrators")
您可能想要:
@Path("getJustAdministrators")
或者:
@Path("./getJustAdministrators")
或更明确地说:
@Path("/service/getJustAdministrators")
答案 1 :(得分:0)
您的servlet类的定义不正确:
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
它应该指向您的Service.java类。
请将该页面用作参考:
https://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html