DEV Community

Cover image for Jersey Configuration with Spring

Jersey Configuration with Spring

Vinay Hegde
A Geek | Tech Enthusiast | Backend Developer | Self made Dev
・2 min read

Hey All!...

This is my first post in this platform ,So be soft in the comment section. In this post I am going to tell how to configure Jersey with Spring.

Have you ever wondered how to configure Jersey in the Spring project?. Didn’t find the exact solution for that?. If Yes, you are in the right place. I assume you are comfortable with Spring and REST APIs. In this article, I am going to tell how to set up the jersey in your project.

Create a Spring Boot project with Don't forget to add the spring-boot-starter-jersey dependency in your pom.xml.

 <groupId> org.springframework.boot</groupId>

After the successful creation of the Spring Boot project Now its time to create resources ( i.e. Resources means you can relate it to Controller what we used to call in SpringMVC). Create each resource as an Interface and implement it. like below.

Let POJO class be :

public class PublicationResource {
    private int publicationId;
    private String publicationTitle;
    private String publicationAuthor;
    // Getters and setters & Constructors

Create a Resource Interface :

public interface InformationResource{
  List<PublicationResource> getPublications();

Now create a class by implementing a Interface.

public class InformationResourceImpl implements InformationResource{
   public List<PublicationResource> getPublications() {
   return Arrays.asList(new PublicationResource(1, “Title1”, 
                        new PublicationResource(2, “Title2”, 

Here I have hardcoded the PublicationResource values. Real time it should come from database, In that case create a Business Service and call that service from by using Inject.

Now the main Configuration part came into play. How you will configure your Resources into jersey. To do that create a one class by extending a ResourceConfig (import org.glassfish.jersey.server.ResourceConfig)where we need to register our resources. There are two ways you can register your resource.

  1. Register a entire base package in :
public class JerseyConfig extends ResourceConfig {
   public JerseyConfig() {

This will register all your resources , though I faced issue while starting a embedded tomcat server with this, So I wont recommend this way , Instead of this we can register all resources explicitly. ( Ahh... Bit hectic ? ).

2.Register a resources Explicitly :

public class JerseyConfig extends ResourceConfig {
   public JerseyConfig() {

Here @ApplicationPath will allow us to define a base path for all our resources. Example Once application starts it will running on port number 8080.We can make a GET request by hitting https://localhost:8080/resource/information. Here you can observe after 8080 , /resource has been added that is coming from @ApplicationPath. All our APIs will have /resources before the endpoint you have defined.

After making HTTP GET - https://localhost:8080/resource/information result would be :

 “publicationId”: 1,
 “publicationTitle”: “Title1”,
 “publicationAuthor”: “Vinay”
 “publicationId”: 2,
 “publicationTitle”: “Title2”,
 “publicationAuthor”: “Hegde”

Cheers!. You have created your first Jersey API. Simple Isn’t it ?:). Please provide your comments or feedback.

Happy coding!.

Discussion (0)