Json based Restful web service with RESTEasy, Jettison and JAXB
In this page you will see support for Json using RESTEasy, Jettison and JAXB APIs. Jettison is a collection of Java APIs
which read and write JSON. In this example we will convert Order object to
json format.
Here is the pom.xml file.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>RestfulWebServices</groupId>
<artifactId>RestfulWebServices</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<repositories>
<repository>
<id>jboss</id>
<url>https://repository.jboss.org/nexus/content/groups/public-jboss/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>2.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.7.Final</version>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jettison-provider</artifactId>
<version>2.3.7.Final</version>
</dependency>
</dependencies>
</project>
|
Web.xml file for your reference:
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- Auto scan REST service -->
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>
org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
</listener-class>
</listener>
<servlet>
<servlet-name>resteasy-servlet</servlet-name>
<servlet-class>
org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>resteasy-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
|
Here is our model object:
package com.java2novice.model;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "order")
public class Order {
private int orderNo;
private String custmer;
private String address;
private String amount;
@XmlAttribute(name = "order-no")
public int getOrderNo() {
return orderNo;
}
public void setOrderNo(int orderNo) {
this.orderNo = orderNo;
}
@XmlElement
public String getCustmer() {
return custmer;
}
public void setCustmer(String custmer) {
this.custmer = custmer;
}
@XmlElement
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@XmlElement(name = "bill-amount")
public String getAmount() {
return amount;
}
public void setAmount(String amount) {
this.amount = amount;
}
}
|
If you notice above, we are using xml related annotations. So you need xml to json converter, use @BadgerFish annotation to
your service method which converts xml to json fromat. Also remember that our restful web service API returning json, so annotate your
service method with @Produces and specify MIME type as application/jon.
package com.java2novice.restful;
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;
import org.jboss.resteasy.annotations.providers.jaxb.json.BadgerFish;
import com.java2novice.model.Order;
@Path("/order-inventory")
public class OrderInventoryService {
@BadgerFish
@GET
@Path("/order/{orderId}")
@Produces(MediaType.APPLICATION_JSON)
public Order getUserById(@PathParam("orderId") Integer orderId){
Order ord = new Order();
ord.setOrderNo(orderId);
ord.setCustmer("Java2Novice");
ord.setAddress("Bangalore");
ord.setAmount("$2000");
return ord;
}
}
|
Try below URL to get xml output:
http://localhost:8080/RestfulWebServices/order-inventory/order/1016

|