DEV Community

Cover image for Jersey Configuration with Spring
Vinay Hegde
Vinay Hegde

Posted on

Jersey Configuration with Spring

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 https://start.spring.io/. Don't forget to add the spring-boot-starter-jersey dependency in your pom.xml.

<dependency>
 <groupId> org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

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 PublicationResource.java be :

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

Create a Resource Interface InformationResource.java :

@Path("information")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface InformationResource{
  @GET 
  List<PublicationResource> getPublications();
}

Now create a class InformationResourceImpl.java by implementing a InformationResource.java Interface.

@Named
public class InformationResourceImpl implements InformationResource{
   @Override
   public List<PublicationResource> getPublications() {
   return Arrays.asList(new PublicationResource(1, “Title1”, 
                       “Vinay”),
                        new PublicationResource(2, “Title2”, 
                       “Hegde”));
}

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 InformationResourceImpl.java 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 JerseyConfig.java 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 JerseyConfig.java :
@Configuration
@ApplicationPath(“/resource”) 
public class JerseyConfig extends ResourceConfig {
   public JerseyConfig() {
      packages(“your.base.package”);
   }
}

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 :

@Configuration
@ApplicationPath(“/resource”)
public class JerseyConfig extends ResourceConfig {
   public JerseyConfig() {
      register(InformationResource.class);
   }
}

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!.

Top comments (0)