简体   繁体   English

找不到REST资源

[英]Can't find REST resource

So i'm trying to develop web app which has Java Class names "Service.java" which is my REST Service. 因此,我正在尝试开发具有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;
}

My main.js file, which is linked to my welcome page, looks like this: 我的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);
        }
    });
}

And i get error in 'inspect' like this: 我在这样的“检查”中遇到错误:

http://localhost:8080/eTicket/rest/service/getJustAdministrators 404 (Not Found) http:// localhost:8080 / eTicket / rest / service / getJustAdministrators 404(未找到)

My web.xml file looks like this: 我的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>

Why it can't find my resource method?? 为什么找不到我的资源方法?

Your sub-path may be trying to bind to the root, just like /service is. 您的子路径可能试图绑定到根目录,就像/ service一样。

@Path("/getJustAdministrators")

You may want: 您可能想要:

@Path("getJustAdministrators")

Or: 要么:

@Path("./getJustAdministrators")

Or even more explicitly: 或更明确地说:

@Path("/service/getJustAdministrators")

The definition of your servlet class is not correct: 您的Servlet类的定义不正确:

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

It should be pointing to your Service.java class. 它应该指向您的Service.java类。

Please use this page as reference: 请使用此页面作为参考:

https://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html https://docs.oracle.com/cd/E13222_01/wls/docs92/webapp/configureservlet.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM