JavaREST Web Services with JAX-RS, JSON, and JAXB: Part Two

REST Web Services with JAX-RS, JSON, and JAXB: Part Two content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

This is the second article in my series on REST using Apache Wink. My previous article covered using JAX-RS (REST web services) using Apache Wink. This programming tutorial uses the same service that we built in part one to return complex types. These types include XML using JAXB and JSON using Jackson as the stream reader or stream writer.

Before we begin, consider refreshing your knowledge of some of the important terminologies and techniques for REST and web services, as showcased in part one of this series of starting JAX-RS/Rest web services using Apache Wink. Make sure you have setup the JAX-RS/REST project as described in the previous article before you go any further.

How to Return XML Using JAXB

The class, whose object has to be returned to the client, must first be annotated using the following annotations:

  • XMLRootElement: The root element of the xml that has to be returned at the class level.
  • XMLAttribute: An attribute of the root element that has to be returned.
  • XMLElement: An element contained within the root element.

We can do this using nesting to allow multiple levels of nested objects, which get returned to the invoker. Observe the code below:

 import javax.xml.bind.annotation.XmlAttribute;   
  * @author sumith_puri   
 public class Product {   
   long id;   
   String name;   
   public long getId() {   
     return id;   
   public void setId(long id) { = id;   
   public String getName() {   
     return name;   
   public void setName(String name) { = name;   
   public String toString() {   
     String productStr="ID:" + + ", NAME: " +;   
     return productStr;   

The respective methods now need to be annotated to return XML using @Produces.

    public Product getProductById(@PathParam(value="id") long id) {   
      Product product = persistenceManager.getProduct(id);   
      return product;   

We can access this method now using a REST Client:

  public void invokeJAXBGET(long id) {   
       System.out.println("Testing JAXB GET command....");      
       RestClient restClient = new RestClient(clientConfig);   
       Resource resource = restClient.resource(REST_WEB_SERVICE+"/"+id);   
       ClientResponse response=resource.accept(MediaType.APPLICATION_XML).get();   
       System.out.println("...JAXB GET command is successful");   

Next, open an Internet browser and navigate to: http://localhost:8080/products/rest/product/3.

  <product id="3">  
 <name>Sumith Puri</name>  

How to Return JSON Using Jackson

Jackson is an extension of JAXB used to return JSON from REST web services. It can be used as a mapper to convert from JAXB. To do so, we must first include the relevant Jackson JARs, as shown in the image below:

REST Web Services with Apache Wink

We also have to write an application class to register the mapper. Here is what that looks like in Java:

  import java.util.HashSet;   
  import java.util.Set;   
  import org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider;   
  import org.codehaus.jackson.xc.JaxbAnnotationIntrospector;   
  public class ProductApplication extends Application {   
    public Set<Class<?>> getClasses() {   
       Set<Class<?>> classes = new HashSet<Class<?>>();   
       return classes;   
    public Set<Object> getSingletons() {   
       Set<Object> s = new HashSet<>();   
       ObjectMapper objMapper = new ObjectMapper();   
       AnnotationIntrospector primary = new JaxbAnnotationIntrospector();   
       AnnotationIntrospector secondary = new JaxbAnnotationIntrospector();   
       AnnotationIntrospector pair = AnnotationIntrospector.pair(primary, secondary);   
       JacksonJaxbJsonProvider jaxbProvider = new JacksonJaxbJsonProvider();   
       return s;   

Next, change the Apache Wink configuration in the file web.xml as follows:

   <?xml version="1.0" encoding="UTF-8"?>   
 <web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">   
       <!-- <param-name>applicationConfigLocation</param-name> -->   
       <!-- <param-value>/WEB-INF/application</param-value> -->   

The next step is to add the relevant method to the web service and mark that it returns JSON. Here is the code to achieve this:

    public Product getProductJsonById(@PathParam(value="id") long id) {   
       Product product = persistenceManager.getProduct(id);   
       return product;   

Now, add the method to the client to test out the method and see if it works:

  public void invokeJSONGET(long id) {   
       System.out.println("Testing JSON GET command....");      
       RestClient restClient = new RestClient(clientConfig);   
       Resource resource = restClient.resource(REST_WEB_SERVICE+"/json/"+id);   
       ClientResponse response=resource.accept(MediaType.APPLICATION_JSON).get();   
       System.out.println("...JSON GET command is successful");   

In your Internet browser, navigate to the following address: http://localhost:8080/products/rest/product/json/3

 {"id":3,"name":"Sumith Puri"}  

There you have it – you have successfully started a JAX-RS/REST web service using Apache Wink!

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories