REST Jersey Maven JSON Tutorial

REST Jersey maven JSON tutorial will drive How to create JSON webservices with Jersey“.  Go through below tutorials to get idea of REST web  services. You can download  REST JSON Example code at bottom of tutorial.

Installations:

  • Eclipse
  • Jersey version : 1.19.3
  • Maven 4
  • Jackon :1.9.10

What are required to configure JSON web services?

  • Jackson dependency JAR files
  • com.sun.jersey.api.json.POJOMappingFeature parameter need to set as “true”

REST JSON Tutorial:

  • In Eclipse,  click on File -> New -> Maven Project -> select simple project
  • In artifacts wizard, give the groupId and artifacts id as shown in below REST JSON Tutorial
  • Update the pom.xml with below Jersey Maven  dependencies:
    1. <dependency>
       <groupId>com.sun.jersey</groupId>
       <artifactId>jersey-bundle</artifactId>
       <version>1.19.3</version>
       </dependency>
       <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-mapper-asl</artifactId>
       <version>1.9.10</version>
       </dependency>
       <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-core-asl</artifactId>
       <version>1.9.10</version>
       </dependency>
       <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-xc</artifactId>
       <version>1.9.10</version>
       </dependency>
       <dependency>
       <groupId>org.codehaus.jackson</groupId>
       <artifactId>jackson-jaxrs</artifactId>
       <version>1.9.10</version>
       </dependency>
       <dependency>
       <groupId>javax.servlet</groupId>
       <artifactId>javax.servlet-api</artifactId>
       <version>3.1.0</version>
       <scope>provided</scope>
       </dependency>
  • create web.xml in webapp and add the below servlet configuraiton
    1. <servlet>
       <servlet-name>JerseyRESTServlet</servlet-name>
       <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
            <init-param>
             <param-name>com.sun.jersey.config.property.packages</param-name>
             <param-value>org.javasavvy.rest</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>JerseyRESTServlet</servlet-name>
            <url-pattern>/rest/*</url-pattern>
       </servlet-mapping>
  • Create package “org.javasavvy.rest” and create controller and pojo classes into that
  • Now the project structure will be like:
  • Now create the CountryResource.java and Country modal like below
  • Country.java modal

Now Create the CountryResource.java REST Resource like below and every method need to annotate with

  • @Consumes(MediaType.APPLICATION_JSON) to post the JSON object in the request
  •  @Produces(MediaType.APPLICATION_JSON) to get JSON response
@Path("/country")
public class CountryResource {
 
 @GET
 @Path("/list")
 @Produces(MediaType.APPLICATION_JSON)
 public List<Country> getCoutries(){
      Country ctr = new Country();
        ctr.setCode("IN");
        ctr.setName("India");
        ctr.setCountryId(101);
      Country ctr1 = new Country();
        ctr1.setCode("US");
        ctr1.setName("United Stated");
        ctr1.setCountryId(102);
     Country ctr2 = new Country();
       ctr2.setCode("UK");
       ctr2.setName("United Kindom");
       ctr2.setCountryId(103);
     List<Country> list = new ArrayList<Country>();
       list.add(ctr);
       list.add(ctr1);
       list.add(ctr2);
  return list;
 }

 @GET
 @Path("/info/{countryCode}")
 @Produces(MediaType.APPLICATION_JSON)
 public Country getCountryInfo(@PathParam("countryCode") String countryCode){
     Country ctr = new Country();
     ctr.setCode("IN");
     ctr.setName("India");
     ctr.setCountryId(101);
 
    return ctr;
 }
 @POST
 @Path("/addCountry")
 @Consumes(MediaType.APPLICATION_JSON)
 @Produces(MediaType.APPLICATION_JSON)
 public Country addCountry(Country country){
     System.out.println("in adding country");
     return country;
 }
}

 

Now deploy the changes by running maven install and deploy directly to tomcat server and copy the generated war file into tomcat webapps folder.

  • Request URL: Now access the URL “http://localhost:8080/rest-json-tutorial/rest/country/list” to ge the list of counties
  • Response:
    •  [{"countryId":101,"name":"India","code":"IN"},
       {"countryId":102,"name":"United Stated","code":"US"},
       {"countryId":103,"name":"United Kindom","code":"UK"}]

JSON Post Request:

Download REST client in chrome and access the URL like below:

  • Set content-type as “application/json”
  • Raw payload as:
    • {
      "countryId": 101,
      "name": "India",
      "code": "IN"
      }

RequestJSON Post Request

 

Click here to download REST JSON Tutorial