cbohannon
cbohannon

Reputation: 261

Jersey JAX-RS ResourceConfig with Tomcat

I have a basic REST web service that works but I do have a question. Here is a brief code snip.

package com.my.app;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ApplicationPath;

@ApplicationPath("api")
public class RestApplication extends ResourceConfig {
    RestApplication() {
        packages("com.my.app");
    }
}

And

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>The name of my service!</display-name>
</web-app>

I have been digging around in the Jersey documentation at https://jersey.java.net/apidocs/2.25.1/jersey/org/glassfish/jersey/server/ResourceConfig.html and I haven't found a way to set the Tomcat display name or version. Now I can just set those parameters in the web.xml and that works just fine but I would rather set the parameters in my class that extends ResourceConfig and get rid of the web.xml altogether. Is this possible or should I just stick with using the web.xml? Any suggestions would be greatly appreciated.

Upvotes: 1

Views: 3744

Answers (1)

cassiomolin
cassiomolin

Reputation: 130857

The ResourceConfig class does not aim to replace the web.xml file.

What is the ResourceConfig class for?

The ResourceConfig class is part of the Jersey API and provides advanced capabilities to simplify registration of JAX-RS components, such as scanning for root resource and provider classes in a provided classpath or a in a set of package names.

It extends the Application class from the JAX-RS API. For more details about the Application class, refer to this answer.

What is the web.xml file for?

The web.xml is a deployment descriptor for servlet container based applications. It instructs the servlet container which classes (servlets, filters and listeners) must be loaded, which properties should be set in the context, etc.

Since Servlet 3.0, you don't even need the web.xml for simple deployments. Most of the configurations, such as registering servlets, filters and listeners can be done via annotations.

However, the web.xml is still necessary if you want to set the <display-name> of your web application. So far, there's no annotation that can replace that tag.

What your web.xml could be like

Apache Tomcat 8 is compatible with the Servlet 3.1 specification (check the Tomcat documentation for more details), so your web.xml can be like:

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <display-name>Web Application Name</display-name>
</web-app>

Upvotes: 4

Related Questions