Reputation: 2459
Currently, I know two ways:
<init-param>
on you web.xmlResourceConfig
and add it on your web.xmlI 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
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.
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.
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