<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Vinay Hegde</title>
    <description>The latest articles on Forem by Vinay Hegde (@vinayhegde2013).</description>
    <link>https://forem.com/vinayhegde2013</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F432800%2F525d1eee-5e1e-41a9-845b-786f02837044.png</url>
      <title>Forem: Vinay Hegde</title>
      <link>https://forem.com/vinayhegde2013</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/vinayhegde2013"/>
    <language>en</language>
    <item>
      <title>Jersey Configuration with Spring</title>
      <dc:creator>Vinay Hegde</dc:creator>
      <pubDate>Wed, 15 Jul 2020 17:49:14 +0000</pubDate>
      <link>https://forem.com/vinayhegde2013/jersey-configuration-with-spring-4514</link>
      <guid>https://forem.com/vinayhegde2013/jersey-configuration-with-spring-4514</guid>
      <description>&lt;p&gt;Hey All!...&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Create a Spring Boot project with &lt;strong&gt;&lt;a href="https://start.spring.io/"&gt;https://start.spring.io/&lt;/a&gt;&lt;/strong&gt;. Don't forget to add the &lt;strong&gt;spring-boot-starter-jersey&lt;/strong&gt; dependency in your &lt;code&gt;pom.xml&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
 &amp;lt;groupId&amp;gt; org.springframework.boot&amp;lt;/groupId&amp;gt;
 &amp;lt;artifactId&amp;gt;spring-boot-starter-jersey&amp;lt;/artifactId&amp;gt;
&amp;lt;/dependency&amp;gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Let POJO class &lt;code&gt;PublicationResource.java&lt;/code&gt; be :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class PublicationResource {
    private int publicationId;
    private String publicationTitle;
    private String publicationAuthor;
    // Getters and setters &amp;amp; Constructors
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Create a Resource Interface &lt;code&gt;InformationResource.java&lt;/code&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Path("information")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public interface InformationResource{
  @GET 
  List&amp;lt;PublicationResource&amp;gt; getPublications();
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now create a class &lt;code&gt;InformationResourceImpl.java&lt;/code&gt; by implementing a &lt;code&gt;InformationResource.java&lt;/code&gt; Interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Named
public class InformationResourceImpl implements InformationResource{
   @Override
   public List&amp;lt;PublicationResource&amp;gt; getPublications() {
   return Arrays.asList(new PublicationResource(1, “Title1”, 
                       “Vinay”),
                        new PublicationResource(2, “Title2”, 
                       “Hegde”));
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;InformationResourceImpl.java&lt;/code&gt; by using Inject.&lt;/p&gt;

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

&lt;ol&gt;
&lt;li&gt;Register a entire base package in &lt;code&gt;JerseyConfig.java&lt;/code&gt; :
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Configuration
@ApplicationPath(“/resource”) 
public class JerseyConfig extends ResourceConfig {
   public JerseyConfig() {
      packages(“your.base.package”);
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;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 ? ).&lt;/p&gt;

&lt;p&gt;2.Register a resources Explicitly :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Configuration
@ApplicationPath(“/resource”)
public class JerseyConfig extends ResourceConfig {
   public JerseyConfig() {
      register(InformationResource.class);
   }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here &lt;strong&gt;@ApplicationPath&lt;/strong&gt; 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 &lt;a href="https://localhost:8080/resource/information"&gt;https://localhost:8080/resource/information&lt;/a&gt;. Here you can observe after 8080 , &lt;code&gt;/resource&lt;/code&gt; has been added that is coming from &lt;strong&gt;@ApplicationPath&lt;/strong&gt;. All our APIs will have &lt;code&gt;/resources&lt;/code&gt; before the endpoint you have defined.&lt;/p&gt;

&lt;p&gt;After making HTTP GET - &lt;a href="https://localhost:8080/resource/information"&gt;https://localhost:8080/resource/information&lt;/a&gt; result would be :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
 {
 “publicationId”: 1,
 “publicationTitle”: “Title1”,
 “publicationAuthor”: “Vinay”
 },
 {
 “publicationId”: 2,
 “publicationTitle”: “Title2”,
 “publicationAuthor”: “Hegde”
 }
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Cheers!. You have created your first Jersey API. Simple Isn’t it ?:). Please provide your comments or feedback.&lt;/p&gt;

&lt;p&gt;Happy coding!.&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>jersey</category>
    </item>
  </channel>
</rss>
