jersey

Configuring JAX-RS in Jersey

Java Jersey CORS filter for Cross Origin Requests

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

    org.glassfish.jersey.containers jersey-container-servlet ${jersey.version} org.glassfish.jersey.media jersey-media-json-jackson ${jersey.version} 2.22.2 UTF-8
  2. In Web.xml, add the following code

    jersey-serlvet org.glassfish.jersey.servlet.ServletContainer jersey.config.server.provider.packages com.stackoverflow.service
     <!-- 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>
    jersey-serlvet /api/*
  3. 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.


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow