Configuring JAX-RS in Jersey

Other topics

Java Jersey CORS filter for Cross Origin Requests

@Provider
public class CORSResponseFilter implements ContainerResponseFilter {

    public void filter(
        ContainerRequestContext requestContext,
        ContainerResponseContext responseContext
    ) throws IOException {
        MultivaluedMap<String, Object> headers = responseContext.getHeaders();
        headers.add("Access-Control-Allow-Origin", "*"); //Allow Access from everywhere   
        headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");            
        headers.add("Access-Control-Allow-Headers", "X-Requested-With, Content-Type");
    }
}

Note that the Access-Control-Allow-Origin is only useful at OPTIONS responses.

Java Jersey Configuration

This example illustrates how to configure Jersey so that you can begin using it as a JAX-RS implementation framework for your RESTful API.

Assuming that you have already installed Apache Maven, follow these steps to set up Jersey:

  1. Create maven web project structure, in terminal (windows) execute the following command

mvn archetype:generate -DgroupId= com.stackoverflow.rest -DartifactId= jersey-ws-demo -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

Note: To support Eclipse, use Maven command : mvn eclipse:eclipse -Dwtpversion=2.0

  1. Go to the folder where you created your maven project,in your pom.xml, add the required dependencies
<dependencies>
    <!-- Jersey 2.22.2 -->
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>${jersey.version}</version>
    </dependency>
    <!-- JSON/POJO support -->
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>${jersey.version}</version>
    </dependency>
</dependencies>

<properties>
    <jersey.version>2.22.2</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
  1. In Web.xml, add the following code
<servlet>
    <servlet-name>jersey-serlvet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <!-- Service or resources to be placed in the following package --> 
        <param-value>com.stackoverflow.service</param-value>
    </init-param>
   
    <!-- Application configuration, used for registering resources like filters  -->
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>com.stackoverflow.config.ApplicationConfig</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
 </servlet>

 <!-- Url mapping, usage-http://domainname:port/appname/api/ -->
 <servlet-mapping>
     <servlet-name>jersey-serlvet</servlet-name>
     <url-pattern>/api/*</url-pattern>
 </servlet-mapping>
  1. The ApplicationConfig class
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        register(OtherStuffIfNeeded.class);
    }
}

It should also be noted that if you want to go with no web.xml, you could simply get rid of it, and add @ApplicationPath("/api") on top of the ApplicationConfig class.

@ApplicationPath("/api")
public class ApplicationConfig extends ResourceConfig {
    public ApplicationConfig() {
        // this call has the same effect as
        // jersey.config.server.provider.packages
        // in the web.xml: it scans that packages for resources and providers. 
        packages("com.stackoverflow.service");
    }
}
  1. Build and deploy your maven project.
  2. You can now set up your Java RESTful webservice (JAX-RS) classes to use Jersey's jars.

Contributors

Topic Id: 7012

Example Ids: 4802,12837

This site is not affiliated with any of the contributors.