AdSenseHeader



       Jersey toolkit is implemented based on JAX-RS Api, it is useful to implement the RESTful Webservices. This  tutorial shows how to implement the jersey sample application.
Fallow the below steps to implement the jersey sample application.

  •        First  Create the Maven Web based project using Eclipse fallow this post

     The project structure looks like as fallows.
·     

    The pom.xml file dependencies
 
  <dependencies>
        <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
       </dependency>
   
       <dependency>
              <groupId>org.glassfish.jersey.containers</groupId>
              <artifactId>jersey-container-servlet</artifactId>
              <version>2.5.1</version>
       </dependency>
       <!-- Required only when you are using JAX-RS Client -->
       <dependency>
              <groupId>org.glassfish.jersey.core</groupId>
              <artifactId>jersey-client</artifactId>
              <version>2.5.1</version>
       </dependency>
       <dependency>
              <groupId>com.fasterxml.jackson.jaxrs</groupId>
              <artifactId>jackson-jaxrs-json-provider</artifactId>
              <version>2.2.3</version>
       </dependency>
   
  </dependencies>
  • ·         First run the pom.xml file with maven install command using Eclipse. The fallowing snapshot illustrates the  running the pom.xml file using maven install command.


·         Web.xml file of jersey Hello world Example.

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
 
  <servlet>
       <servlet-name>Jersey Web Application</servlet-name>
       <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
       <init-param>
              <param-name>jersey.config.server.provider.packages</param-name>
              <param-value>com.sparktheprograms.jersey</param-value>
       </init-param>
       <init-param>
              <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
              <param-value>true</param-value>
       </init-param>
       <load-on-startup>1</load-on-startup>
 </servlet>

        <servlet-mapping>
              <servlet-name>Jersey Web Application</servlet-name>
              <url-pattern>/rest/*</url-pattern>
       </servlet-mapping>
</web-app>

In above web.xml file
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.sparktheprograms.jersey</param-value>

In the above param-value put your based on your package java package.
In the url-pattern also put based on your interest.I put the “ /rest” pattern.

Rest Service Class
package com.sparktheprograms.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("helloworld")
public class JerseySample {
      
       @GET
       @Produces(MediaType.TEXT_PLAIN)
       public String helloMessage() {
              return "Hey, This is Jersey !";
       }
      
       @GET
       @Path("/message")
       @Produces(MediaType.APPLICATION_JSON)
       public Message message() {
              Message message = new Message();
              message.setSite("www.9threes.com");
              message.setMessage("JERSEY Sample Application");
              return message;
       }
      
}


Bean Class
package com.sparktheprograms.jersey;

public class Message {
      
       String site;
       String message;
      
       public String getSite() {
              return site;
       }
       public void setSite(String site) {
              this.site = site;
       }
       public String getMessage() {
              return message;
       }
       public void setMessage(String message) {
              this.message = message;
       }
      
      
}

Demo




3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. What a piece of code have you written, it's impressive! Literally, all the steps that I have taken so far in order to write some programs in such creative fashion have not been so successful. But at least I have confidence that I'm good with the basics, these tutorials on java core here provided pieces that are actually quite common issues for beginners. And obviously I know how to write simple hello-world thing, but nevertheless I need more practicing here and now I'm quite far from this skills of yours

    ReplyDelete

 
Top