Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL of a Jersey Application using ResourceConfig without web.xml

I migrated from web.xml to totally Java configuration using ResourceConfig with Jersey 2.7 and deploying on Tomcat 7. After that I am not able to reach the services anymore by using the same urls that I was using with the web.xml approach. I don't understand how the ResourceConfig is affecting the paths.

My previous 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_3_0.xsd"    
version="3.0">
<servlet>
    <servlet-name>my.app</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.mypackage.resource,com.mypackage.providers</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.scanning.recursive</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>        
    </init-param>
    <init-param>
        <param-name>org.glassfish.jersey.server.ServerProperties.BV_SEND_ERROR_IN_RESPONSE</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>my.app</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

My configuration class that extends ResourceConfig is:

MyRESTAPIApp.java

@ApplicationPath("")
public class MyRESTAPIApp extends ResourceConfig{
    public MyRESTAPIApp () {
        packages("com.mypackage.resource", "com.mypackage.providers");
        register(org.glassfish.jersey.filter.LoggingFilter.class);
        property("jersey.config.beanValidation.enableOutputValidationErrorEntity.server", "true");
    }
}

one of my resources is:

FlagResource.java

@Path("my-resource")
public class FlagResource {
private MyService myService = new MyService();

@GET
@Produces(MediaType.APPLICATION_JSON)
public FlagResource getFlagResource(@NotNull @QueryParam("level") Long level) {
    FlagResource flagResource = myService.getFlagResource(level);
    return flagResource;
}

}

The war that I am generating is called: my.app.war.

Tomcat was taking the web context root path from the name of the war file as usual, but I don't know if that changes when using Java code based configuration.

GET http://localhost:8080/my.app/my-resource?level=1

Returns a 404

like image 404
raspacorp Avatar asked Apr 08 '14 21:04

raspacorp


1 Answers

Actually I solved this by adding "/" as the value of the @ApplicationPath annotation, I thought it was not necessary because the API documentation says the following for @ApplicationPath value param:

Defines the base URI for all resource URIs. A trailing '/' character will be automatically appended if one is not present.

I assumed that leaving an empty String will be equivalent to use @ApplicationPath("/") but it is not.

So this is how the configuration class looks now:

@ApplicationPath("/")
public class MyRESTAPIApp extends ResourceConfig{
    public MyRESTAPIApp () {
        packages("com.mypackage.resource", "com.mypackage.providers");
        register(org.glassfish.jersey.filter.LoggingFilter.class);
        property("jersey.config.beanValidation.enableOutputValidationErrorEntity.server", "true");
    }
}
like image 126
raspacorp Avatar answered Oct 15 '22 01:10

raspacorp