JAX-RS REST @Consumes Example

Previously we have learnt about how to produce different types of responses using @Produces annotation, In this article we will learn how to consume different types of request from the user using @Consumes annotation.

@Consumes annotation is used to specify the MIME media types that a resource can consume. @Consumes can be applied at both class level and method level, If applied on class level all the methods can produce the specified MIME types by default. If it is applied at the method level, it overrides any @Consumes annotations applied at the class level.

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 


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

@Produces Example

We have two methods here consumeXMLRequest() which consumes XML request (MediaType.APPLICATION_XML) and consumeJSONRequest() which consumes json request (MediaType.APPLICATION_JSON) based on the MediaType passed in the in @Consumes annotation

package com.javainterviewpoint;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MediaType;

public class ConsumesExample 
    public void consumeXMLRequest(Student s)
        System.out.println("**Received XML request**");
        System.out.println(" Name : "+s.getName());
        System.out.println(" Age  : "+s.getAge());
    public void consumeJSONRequest(Student s)
       System.out.println("**Received JSON request**");
       System.out.println(" Name : "+s.getName());
       System.out.println(" Age  : "+s.getAge());


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

package com.javainterviewpoint;

import javax.xml.bind.annotation.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;


<?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%20http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd http://java.sun.com/xml/ns/j2ee%20http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" id="WebApp_ID" version="2.4">
 <display-name>Restful Web Application</display-name>

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 :

Use any REST clients for this, i am using “POSTMAN” client here.

Passing XML Request



**Received XML request**
 Name : JavaInterviewPoint
 Age  : 10

Passing JSON Request

**Received JSON request**
 Name : JavaInterviewPoint
 Age  : 11

Leave a Reply

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