简体   繁体   English

具有多个资源类的Spring / Jersey Rest API返回404

[英]Spring/Jersey Rest API with multiple resource class returns 404

I am creating the spring/jersey based rest API and my application returns 404. I know there are many similar posts and I tried the solutions, but my case is a little bit different in that, when I create only one class, app1.class, and specific the path ("/"), it works, but when I add another class and specify another path, which is the subpath (/v1.0/app/request) of app 1, that is also fine. 我正在创建基于spring / jersey的rest API,我的应用程序返回404。我知道有很多类似的文章,并且尝试过解决方案,但是在我只创建一个类app1.class的情况下,我的情况有所不同。 ,并指定路径(“ /”),它可以工作,但是当我添加另一个类并指定另一个路径(即应用1的子路径(/v1.0/app/request))时,也可以。 But when I add another class app3.class, which has path (/v1.0/app/response), then, when I compile the war and put it into the container, tomcat 7, it wont work. 但是,当我添加另一个具有路径(/v1.0/app/response)的类app3.class时,则当我编译war并将其放入容器tomcat 7时,它将无法工作。 none of the defined path can be accessed. 没有定义的路径可以访问。

All resources class are in the package com.example.restservice. 所有资源类都在com.example.restservice包中。

my web.xml looks like this as below: 我的web.xml如下所示:

         <?xml version="1.0" encoding="ISO-8859-1"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web- 
     app_3_0.xsd">


<display-name>publishing-service</display-name>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>
        com.sun.jersey.spi.spring.container.servlet.SpringServlet
    </servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.example.restservice</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

I just checked the log of apache and may found the problem. 我刚刚检查了Apache的日志,可能发现了问题。 I defined a bean in applicationContext.xml, as and I used annocation @autowired to create the instance, but tomcat failed to instantiate this bean, it can not find the com.example.validationserviceImpl, which is not a local class, but in a dependency defined in pom.xml. 我在applicationContext.xml中定义了一个bean,因为我使用了@autowired实例来创建实例,但是tomcat无法实例化此bean,它找不到com.example.validationserviceImpl,它不是本地类,而是在pom.xml中定义的依赖项。 How can I define a bean which is in a jar ? 我该如何定义一个在罐子里的豆子?

One option you might try is to add a level of indirection using sub-resource locators: 您可以尝试的一种选择是使用子资源定位器添加一个间接级别:

@Component
@Path("/")
public class AppResource {
    @Autowired private App1 app1;
    @Autowired private App2 app2;
    @Autowired private App3 app3;

    @Path("")
    public App1 app1Resource() {
        return app1;
    }

    @Path("/v1.0/app/request")
    public App2 app2Resource() {
        return app2;
    }

    @Path("/v1.0/app/response")
    public App3 app3Resource() {
        return app3;
    }
}

Then remove the class-level @Path annotations on classes App1, App2, App3. 然后删除类App1,App2,App3上的类级别@Path批注。 So, for example, App1 becomes: 因此,例如,App1变为:

@Component
public class app1 {
    @GET
    @Path("/health")
    public Response health() {...}
}

Or -- without adding an extra class -- add the sub-resource locators to App1: 或者-不添加额外的类-将子资源定位符添加到App1:

@Component
@Path("/")
public class App1 {
    @Autowired private App2 app2;
    @Autowired private App3 app3;

    @GET
    @Path("/health")
    public Response health() {...}

    @Path("/v1.0/app/request")
    public App2 app2Resource() {
        return app2;
    }

    @Path("/v1.0/app/response")
    public App3 app3Resource() {
        return app3;
    }
}

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

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