mpmp
mpmp

Reputation: 2459

Jersey 2.22: Where should I define the location of REST resources?

Currently, I know two ways:

  1. Specify it as an <init-param> on you web.xml
  2. Create a class that extends ResourceConfig and add it on your web.xml

I have this class that extends ResourceConfig because I needed to register an Application Binder (AbstractBinder) for dependency injection.

Where should I define the location of my REST resources? What's the best practice here?

Upvotes: 0

Views: 310

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209052

In the ResourceConfig, you can call packages("reource.packages"), which will do the same as scanning the package declared inside the <init-param>

public class Config extends ResourceConfig {
    public Config() {
        packages("...");
        register(new AbstractBinder()..);
    }
}

To use the Config class, you have a couple options.

  1. Annotate it with @ApplicationPath("/appPath")

    With this, no web.xml is required. You need to make sure you have the jersey-container-servlet dependency for this to work. The value in the annotation works the same way as the <servlet-mapping> inside the web.xml.

  2. Declare the Config class inside the web.xml

    <servlet>
        <servlet-name>MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.foo.Config</param-value>
        </init-param>
    </servlet>
    ...
    <servlet-mapping>
        <servlet-name>MyApplication</servlet-name>
        <url-pattern>/myPath/*</url-pattern>
    </servlet-mapping>
    

You could also not use the ResourceConfig and register the binder inside a Feature, as discussed here

See Also:

Upvotes: 1

Related Questions