Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

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 withhttps://start.spring.io/. Don't forget to add thespring-boot-starter-jersey dependency in yourpom.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 classPublicationResource.java be :

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

Create a Resource InterfaceInformationResource.java :

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

Now create a classInformationResourceImpl.java by implementing aInformationResource.java Interface.

@Namedpublic 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 fromInformationResourceImpl.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 classJerseyConfig.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 inJerseyConfig.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 hittinghttps://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)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

A Geek | Tech Enthusiast | Backend Developer | Self made Dev
  • Location
    Somewhere in the Universe
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp