简体   繁体   中英

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. 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. none of the defined path can be accessed.

All resources class are in the package com.example.restservice.

my web.xml looks like this as below:

         <?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. 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. 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. So, for example, App1 becomes:

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

Or -- without adding an extra class -- add the sub-resource locators to 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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