Thursday 10, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 30 November 2013

JAX-RS XML Service using JAXB


In this tutorial we are going to see how to convert object to XML using JAXB and return back to user using JAX-RS 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.xmlservice under RESTful-WebService
5. Create Java class JAXBXMLService under com.javatutorialscorner.jaxrs.xmlservice package
JAXBXMLService.java
package com.javatutorialscorner.jaxrs.xmlservice;

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

@Path("/xml")
public class JAXBXMLService {

@GET
@Path("/jaxb/{id}")
@Produces(MediaType.APPLICATION_XML)
public Student getStudentDetail(@PathParam("id") int id) {
Student student = new Student();
student.setName("Ram");
student.setId(id);
student.setDepartment("EEE");
student.setYear(2011);

return student;
}
}

@Produces(MediaType.APPLICATION_XML) annotation will return XML file.Jersey will automatically convert JAXB annotated object into XML.We will see JAXB in detail in upcoming chapters.  

6. Create Java class Student under com.javatutorialscorner.jaxrs.xmlservice package

Student.java

package com.javatutorialscorner.jaxrs.xmlservice;

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;
}

}

This class used to convert object to XML using JAXB and it will  return following XML

<student id="32">
<department>EEE</department>
<name>Ram</name>
<year>2011</year>
</student>

7.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.xmlservice</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

Now you can run the service and access the service by calling the following URL

http://localhost:8080/RESTful-WebService/rest/xml/jaxb/32

you can call the service by using web browser or from client class.

Web browser call

output

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
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String baseURI = "http://localhost:8080/RESTful-WebService";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource service = client.resource(baseURI);
System.out.println("output for JAXB XML Service param");
System.out
.println(service
.path("rest")
.path("xml/jaxb/32")
.get(ClientResponse.class).getEntity(String.class));

}

}

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

output for JAXB XML Service param

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><student id="32"><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 RESTful Web service to consume JSON and Produce XMLIn this tutorial we are going to see how to consume JSON and Produce XML using RESTful web service1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-W… Read More
  • JAX-RS @GET using RESTful webserviceIn this tutorial we are going to see  about RESTful web service’s @GET annotation1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Project called RESTful-WebService3. … Read More
  • RESTful Java Client using HttpURLConnection In this tutorials we are going to see the how to access the RESTful web service using Java built-in client library . Here i am going to create two clients to access GET and POST request GET Request Refer this example for cr… Read More
  • RESTful Web service with Jersey Client In this tutorials we are going to see the how to access the RESTful web service using jersey client API. Here i am going to create two clients to access GET and POST request . Add jersey-client-1.17.1.jar  in build pat… Read More
  • JAX-RS Send List using QueryParam AnnotationIn this tutorial we are going to see how to send list to RESTful web service via URL using @QueryParam1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project as given below. 2. Create the Projec… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: JAX-RS XML Service using JAXB Rating: 5 Reviewed By: eHowToNow