Tuesday 29, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 4 January 2014

JAX-RS RESTful Web service to consume JSON and Produce XML

In this tutorial we are going to see how to consume JSON and Produce XML using RESTful web service
1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project .
2. Create the Project called RESTful-WebService
3. Add the following jar into WEF-INF/lib folder
add jar
4. Create package called com.javatutorialscorner.jaxrs.consumeproduce under RESTful-WebService
5. Create Java class Student under com.javatutorialscorner.jaxrs.consumeproduce package
Student.java
package com.javatutorialscorner.jaxrs.consumeproduce;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "student")
public class Student {


String name;
int id;
String department;
int year;

@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}

@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}

@XmlElement
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}

@XmlElement
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}

}

In above class annotations used for JAXB (to produce XML) and same class used to consume JSON

6. Create Java class RestConsumeProduce under com.javatutorialscorner.jaxrs.consumeproduce package

RestConsumeProduce.java

package com.javatutorialscorner.jaxrs.consumeproduce;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("restconsumeproduce")
public class RestConsumeProduce {

@POST
@Path("post")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_XML)
public Student getStudentDetail(Student student) {
return student;
}
}

6.Configure Jersey Servlet Dispatcher
you need to configure REST as servlet in web.xml.
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>JAX-RS-Path</display-name>
<servlet>
<servlet-name>jersey-serlvet</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>com.javatutorialscorner.jaxrs.consumeproduce</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-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>

The servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer. The init param com.sun.jersey.config.property.package is used to define in which package jersey will look for the service classes.This package points to your resource class package. URL pattern is the part of base URL.
you can call the service by using client class.

Create Client

Create package called com.javatutorialscorner.jaxrs.client under RESTful-WebService project

Create Client Java Class RESTfulClient under  com.javatutorialscorner.jaxrs.client package

RESTfulClient .java

package com.javatutorialscorner.jaxrs.client;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

public class RESTfulClient {

/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String baseURI = "http://localhost:8080/RESTful-WebService/rest/restconsumeproduce/post";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(baseURI);
String json = "{\"name\":\"Ram\",\"id\":33,\"department\":\"EEE\",\"year\":2011}";
ClientResponse response = service.type("application/json").post(
ClientResponse.class, json);
System.out.println("output for Consume JSON And Produce XML Service");

if (response.getStatus() != 200) {
throw new Exception("Error code : " + response.getStatus());
}

System.out.println("output " + response.getEntity(String.class));

}

}

Now you can run the client and see the following output in console

output for Consume JSON And Produce XML Service

output <?xml version="1.0" encoding="UTF-8" standalone="yes"?><student id="33"><department>EEE</department><name>Ram</name><year>2011</year></student>

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • JAX-RS Get HTTP Headers using @ContextIn this tutorial we are going to see how to get HTTP request header using JAX-RS  RESTful web service @Context 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Proje… Read More
  • JAX-RS Get HTTP Headers using @HeaderParamIn this tutorial we are going to see how to get HTTP request header using JAX-RS  RESTful web service @HeaderParam 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project as given below. 2… Read More
  • JAX-RS @QueryParam with @DefaultValueIn this tutorial we are going to see how set  default value to query parameters in RESTful web service using @DefaultValue 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project.2. Cre… Read More
  • JAX-RS @FormParamIn this tutorial we are going to see how to get HTML form parameters in java method using RESTful web service via URL using @FormParam @FormParam used to bind HTML form parameters  to java method1. Create new Dynamic we… Read More
  • JAX-RS get QueryParam using @Context UriInfoIn this tutorial we are going to see how to retrieve  query parameters in RESTful web service using @Context UriInfo 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JAX-RS RESTful Web service to consume JSON and Produce XML Rating: 5 Reviewed By: eHowToNow