JAX-RS REST @Produces both XML and JSON Example

In my previous article we have learnt how to use @Produces annotation. We have produced either xml or json response but not able to produce both xml and json in a single method. In this example we will learn how to produce multiple media type response depending upon the clients request we will produce response, lets see how we can achieve it.

Folder Structure

  1. Create a Dynamic Web Project RESTful_Example and create a package for our src files com.javainterviewpoint
  2. Place the required jar files under WEB-INF/Lib 

    jersey-bundle-1.18.jar
    jsr311-api-1.1.1.jar
    jaxb-api.jar
    jaxb-impl-2.0.1.jar
    asm-3.1.jar

  3. Create the Java classes Produces_XML_JSON_Example.java and Student.java under  com.javainterviewpoint folder.
  4. Place the web.xml  under the WEB-INF directory

Produces_XML_JSON_Example.java

The getBothResponse() method is capable of producing both xml and json response, if the client is requesting for an xml response it will produce xml response, if json then it will produce json response.

package com.javainterviewpoint;

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

@Path("produceboth")
public class Produces_XML_JSON_Example 
{
    @Path("both")
    @GET
    @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
    public Student getBothResponse()
    {
        Student s = new Student();
        s.setAge(10);
        s.setName("JavaInterviewPoint");
        
        return s;
    }
}

Student.java

We have annotated our Student class with @XmlRootElement marking the root element.

package com.javainterviewpoint;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student 
{
    private String name;
    private int age;
       
    public Student() {
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}

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/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
 <display-name>Restful Web Application</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.javainterviewpoint</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>

We register our jersey container “com.sun.jersey.spi.container.servlet.ServletContainer” in the servlet-class of our web.xml and we will mention the source files location as the value to the init param “com.sun.jersey.config.property.packages” so that the container will scan for annotations of the class files within that package.

Output :

XML Response

URL : http://localhost:8080/RESTful_Example/rest/produceboth/both

In the postman client under "Headers" tab add parameter "Accept" --> "application/xml" to get xml response.
Produces_XML1

JSON Response

URL : http://localhost:8080/RESTful_Example/rest/produceboth/both
In the postman client under "Headers" tab add parameter "Accept" --> "application/json" toget json response.
Produces_JSON1

Leave a Reply

Your email address will not be published. Required fields are marked *