Thursday, March 12, 2015

REST Using Apache Wink (XML/JSON)

[GitHub Repository for Code Samples]

As mentioned earlier, this is  the second in series on REST Using Apache Wink. You can understand how to use complex return types, such as XML using JAXB and Json using Jackson as the stream reader or stream writer.

Firstly, setup the REST project as described in REST Using Apache Wink

1. Returning XML using JAXB
The class whose object that has to be returned to the client has to be first annotated using the following annotations:

1. XMLRootElement
The root element of the xml that has to be returned, at the class level

2. XMLAttribute
An attribute of the root element that has to be returned

3. XMLElement
An element contained within the root element

This can be used in a nested way to allow multiple levels of nested objects to be returned to the invoker.
 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 method(s) now need to 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");  

On the browser, type the url : http://localhost:8080/products/rest/product/3
<product id="3">
<name>Sumith Puri</name>

2. Returning JSON using Jackson
Jackson is an extension over JAXB to return JSON from REST web service. It can be used as the mapper to convert from JAXB. First, include the relevant Jackson JARs:

We also have to write an application class, to register the mapper.

 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 your 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 is to add the relevant method to the service, and mark that it returns JSON. 
      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.
      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");  

On the browser, type the url : http://localhost:8080/products/rest/product/json/3 
{"id":3,"name":"Sumith Puri"}

You may download the war, including source, for reference from here.

[GitHub Repository for Code Samples]


Mohammed Shafeek said...

Hi, I am new to apache win and REST, can you please let me know how can i read the result in standalone java instead of browser

Sumith Kumar Puri said...

hi mohammed, actually with apache wink - the restclient is packaged into the client jar of the distribution.

you may check the javadoc here:

if you are using maven, this may do the trick for dependencies, as provided for the clients:

velraj said...

I read like this type of article only. You mentioned more important points in this article. Thankyou for interesting article.
core java training in chennai
core java Training in Anna Nagar
best c c++ training in chennai
c c++ courses in chennai
C Language Training
javascript training in chennai
Appium Training in Chennai
JMeter Training in Chennai

yuvaprithika said...

Nice blog!! I hope you will share more info like this. I will use this for my studies and research.
Angularjs Training in Chennai
Angularjs Course in Chennai
Ethical Hacking Course in Chennai
Tally Course in Chennai
gst training in chennai
salesforce training institute in chennai
Angularjs Certification in Chennai
Angularjs Training in Chennai
Angularjs Course in Chennai

kiruthika said...

This is really a valuable post... The info shared is helpful and valuable. Thank you for sharing.
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
aviation training in Chennai
air hostess academy in Chennai
Airline Courses in Chennai
Ground staff training in Chennai

Anbarasan14 said...

Fabulous post. Irrespective of the age groups your post is liked by all. Thanks for sharing.

IELTS Coaching in Mulund
IELTS Training in Mulund West
IELTS Courses in Mulund
IELTS Coaching Centres in Mulund
Spoken English Class in Chennai
Spoken English in Chennai
IELTS Coaching in Chennai

Sivanandhana Girish said...

Great info. Thanks for spending your valuable time to share this post.
Education Franchise India
Spoken English Franchise
Franchise For Spoken English Classes
Top Education Franchise In India
Best Education Franchise In India
Computer Education Franchise
Education Franchise India
Computer Center Franchise
Education Franchise Opportunities In India

Adhuntt said...

Great blog thanks for sharing. Are you looking for digital marketing service?
digital marketing company in chennai
seo service in chennai
web designing company in chennai
social media marketing company in chennai

Unknown said...

Excellent blog thanks for sharing Pixies Beauty Shop is unlike any of the other cosmetic shops in Chennai. With tons of exclusive imported brands to choose from and the best value, this is the best shopping destination for your personal and salon needs.

Karuna said...

Nice blog thanks for sharing Karuna Nursery is the Best Plant Nursery in Chennai.
plant nursery in chennai
rental plants in chennai
corporate gardening service in chennai