Issues in JAXRS Rest service using jersey

2019-07-20 15:48发布

问题:

Deployed a demo Rest Service on tomcat 7 using jersey JAXRS API . Developed a resource class

@Path("/supportdata")
public class SupportDataService {
    public SupportDataService() {
        // TODO Auto-generated constructor stub
    }


    @GET
    @Produces(MediaType.APPLICATION_XML)
    public String getSupportData(){
        String xmlSupport=null;

        xmlSupport="<SupportData><Support><key>path1</key><value>value1</value></Support><Support><key>path2</key><value>value2</value></Support></SupportData>";

        return xmlSupport;
    }
}

Sub class of Application

public class RestApplication extends Application {
    public RestApplication() {
        // TODO Auto-generated constructor stub
    }

    @Override
    public Set<Class<?>> getClasses() {
        // TODO Auto-generated method stub

        Set<Class<?>> s=new HashSet<Class<?>>();
        s.add(SupportDataService.class);
        return s;
    }
}
 Web.xml contains

<web-app>
 <servlet>
        <servlet-name>com.jaxrs.RestApplication</servlet-name>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.jaxrs.RestApplication</servlet-name>
        <url-pattern>/resources</url-pattern>
    </servlet-mapping>
</web-app>

Included javax.ws.rs-api-2.0.1.jar from jersey in the project. Deployed it on defaultport of tomcat 7 and calling the url  on local host port 8080 and url 

/RestService/resources/supportdata

getting a 404 error of resource not available.

回答1:

Given...

  1. RestApplication is in the com.jaxrs package

  2. You have the required dependencies [1]

  3. No other unknown issues related to something you're not showing us

All you need to do to get this to work...

is change

 <url-pattern>/resources</url-pattern>

to

 <url-pattern>/resources/*</url-pattern>

The resource should be accessible via http://localhost:8080/yourapp/resources/supportdata

Tested this and it works fine. /resources is limiting the url pattern to strictly /resources. When you add the /* you are saying anything with the /resources prefix.


[1] :

Using Maven, this is the only dependency I used

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.13</version>
</dependency>

Not using Maven (ahhhh, whyyy?):

I suggest you download the RI Bundle from here and include all the jars into your project.


And just for others scratching their head about the OP's configuration, this is from the JAX-RS spec:

If an Application subclass is present:

  • If there is already a servlet that handles this application. That is, a servlet that has an initialization parameter named

           javax.ws.rs.core.Application

    whose value is the fully qualified name of the Application subclass, then no additional configuration steps are required by the JAX-RS implementation.

  • If no servlet handles this application, JAX-RS implementations are REQUIRED to dynamically add a servlet whose fully qualified name must be that of the Application subclass. If the Application subclass is annotated with @ApplicationPath, implementations are REQUIRED to use the value of this annotation appended with "/*" to define a mapping for the added server. Otherwise, the application MUST be packaged with a web.xml that specifies a servlet mapping. For example, if org.example.MyApplication is the name of the Application subclass, a sample web.xml would be:

1   <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
2                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3                        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
4                        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
5       <servlet>
6            <servlet-name>org.example.MyApplication</servlet-name>
7       </servlet>
8       <servlet-mapping>
9            <servlet-name>org.example.MyApplication</servlet-name>
10           <url-pattern>/myresources/*</url-pattern>
11      </servlet-mapping>
12  </web-app>