3
votes

I am working on project to upgrade my existing web application which is developed in JAX-RS 1.12 and running in tomcat 7. Now I am upgrading it to JAX-RS2.0. During tomcat server startup my resources are not getting loaded ?

Below are the details. Added below jars for JAX-RS 2.0

jersey-client-2.0-m07-1 jersey-common-2.0-m07-1 jersey-container-servlet-2.0-m07-1 jersey-container-servlet-core-2.0-m07-1 jersey-server-2.0-m07-1 javax.ws.rs-api-2.0-m10 osgi-resource-locator-1.0.1 javax.inject-2.1.28 javax.inject-1 hk2-utils-2.1.28 hk2-locator-2.1.28 hk2-api-2.1.28 guava-13.0 cglib-2.1.28 asm-all-repackaged-2.1.28

In 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
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>ConfigLiteJersey2</display-name>

<!-- Jersey Servlet to Support JAXRS Services -->
<servlet>
    <servlet-name>ConfigLiteServices</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>        
     <init-param>
        <param-name>javax.ws.rs.core.Application</param-name>
        <param-value>com.cisco.config.resource</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
     <servlet-mapping>
    <servlet-name>ConfigLiteServices</servlet-name>
    <url-pattern>/config/*</url-pattern>
</servlet-mapping>

My Resource File

@Path("/configset")
public class ConfigSetResource {   
    @POST
@Path("/id{configsetid: (/[^/]+?)?}")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public ConfigSetResponse getConfigSet(@PathParam("configsetid") String sConfigSetId)    throws    Exception {
     //Code
    }
    }

Trying to access my resource API using below URL ipaddress:8080/ConfigLiteJersey2/config/configset/id

Getting HTTP status 404 Not found.

Looks like I am not giving right servletclass mapping in web.xml. Please share your thoughts on this

4

4 Answers

2
votes

If you want Jersey to scan your package for resources, change your param-name to:

<param-name>jersey.config.server.provider.packages</param-name>
1
votes

I am using Jersey 2.15, and below is working configuration:

<servlet>
    <servlet-name>emper</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.le.config.ResourceConfiguration</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.le.exceptions</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>emper</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet>  

Great thing that I came to know was, we can register provider class like Global Exception handler. hence com.le.exceptions contain my provider class which implements ExceptionMapper provided by Jersey.

Another thing, We need to create a subclass of org.glassfish.jersey.server.ResourceConfig and register it as our jax-rs application.

0
votes

If you are using Servlet version 3.0 I would suggest following the example in the jersey manual here: https://jersey.java.net/documentation/latest/deployment.html#deployment.servlet.3.descriptor

Create a class that implements javax.ws.rs.core.Application, say org.foo.rest.ConfigLiteApplication. Then make your web.xml like the following (adapted from the jersey page slightly to match your example):

<web-app>
    <servlet>
        <servlet-name>org.foo.rest.ConfigLiteApplication</servlet-name>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>org.foo.rest.ConfigLiteApplication</servlet-name>
        <url-pattern>/config/*</url-pattern>
    </servlet-mapping>
</web-app>

This should work on both JAX-RS 1.1 and JAX-RS 2.0, and should be neutral to the jersey version as you never need to name any jersey classes. The ConfigLiteApplication class should load the resources you want to include, for example adapted from http://tomee.apache.org/examples-trunk/rest-example-with-application/README.html:

@ApplicationPath("/config")
public class ConfigLiteApplication extends Application {
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>(Arrays.asList(ConfigSetResource.class));
    }
}

A modern IDE will likely be able to manage this class for you.

Also, if I were you I would be careful to check this regular expression match if you are hoping to match on /id

@Path("/id{configsetid: (/[^/]+?)?}")

I would consider splitting this up into multiple functions or otherwise working to try and avoid this kind of regex. For example

@POST
@Path("/id")
public ConfigSetResponse getConfigSet() {
     return this.getConfigSet(null);
}
@POST
@Path("/id/{configsetid}")
public ConfigSetResponse getConfigSet(@PathParam("configsetid") String sConfigSetId) {
     //Code
}
0
votes

After long Google search, I Configured mine this way and it worked perfectly

build.gradle should be something like this

compile 'log4j:log4j:1.2.7'
compile 'org.slf4j:slf4j-log4j12:1.6.6'
compile 'org.glassfish.jersey.containers:jersey-container-servlet:2.6'

ApplicationConfig.java file should be something like this

@ApplicationPath("/app")
public class ApplicationConfig extends ResourceConfig {

    public ApplicationConfig() {
        packages("com.flexisaf.resources");

    }
}

Your web.xml file should be something like this

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!-- Default page to serve -->

    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <display-name>SAFHRMS</display-name>
    <servlet>
        <servlet-name>com.flexisaf.safhrms.client.config.ApplicationConfig</servlet-name>

        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>com.flexisaf.safhrms.client.config.ApplicationConfig</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>safhrms.jsp</welcome-file>
    </welcome-file-list>

</web-app>

This solves my problems..Thanks