JAX-RS @Consumes with Example – RESTful Web Services Tutorial

7 years ago Lalit Bhagtani 0

In JAX-RS @Consumes annotation is used to specify the MIME media type ( input) that a resource method can consume from the client.

@Consumes annotation can be applied at both class level and method level. If it is applied at the class level, all the methods in a resource will consume the specified MIME type by default and if it is applied at the method level, then annotated method will consume the specified MIME type by overriding any @Consumes annotations applied at the class level.

If any method in a resource is not able to consume the specified MIME type, then Jersey runtime sends back an HTTP “415 Unsupported Media Type” error to the client.

The value of @Consumes annotation is an array of String of MIME types. For example:

@Consumes(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_XML)

Let’s try to understand it with an example.

Example 1 :-

In this example of JAX-RS @Consumes, we will hit this URL <base URL>/books with HTTP PUT method to save the book resource sent in plain text format.

import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("books")
public class ConsumesAnnotationExample {

  @PUT
  @Consumes(MediaType.TEXT_PLAIN)
  @Produces(MediaType.TEXT_PLAIN)
  @Path("/{id}")
  public Response getAllBooks(@PathParam("id") String bookId,String bookData){
    String[] data = bookData.split(",");
    Book book = new Book();
    book.setId(bookId);
    book.setName(data[0]);
    book.setVolumeNumber(Integer.parseInt(data[1]));
    book.setAuthorName(data[2]);
    return Response.status(200)
		   .entity("Saved :- "+bookData).build();
  }
	
}



Result :-

JAX-RS @Consumes

Example 2 :-

Book.java

Create a bean class “Book” with few class members. This class will be used as an entity to receive a request from client in JSON format. We also annotated our bean class with @XmlRootElement marking it as a root element, so that we can use same bean class to receive a request from client in XML format.

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Book {

  private String id;
	
  private String name;
	
  private String authorName;
	
  private int volumeNumber;
	
  public String getId() {
    return id;
  }
	
  public String getName() {
    return name;
  }
	
  public void setName(String name) {
    this.name = name;
  }
	
  public String getAuthorName() {
    return authorName;
  }
	
  public void setAuthorName(String authorName) {
    this.authorName = authorName;
  }
	
  public int getVolumeNumber() {
    return volumeNumber;
  }
	
  public void setVolumeNumber(int volumeNumber) {
    this.volumeNumber = volumeNumber;
  }	
	
}

Dependency

Add one dependency in pom file of your project. This dependency is required by jersey library to convert your bean class in to JSON format or vice versa.

<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-moxy</artifactId>
    <version>2.26-b03</version>
</dependency>

In this example of JAX-RS @Consumes, we will hit this URL <base URL>/books to save the book resource sent in JSON format.

import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("books")
public class ConsumesAnnotationExample {

  @PUT
  @Consumes(MediaType.APPLICATION_JSON)
  @Produces(MediaType.TEXT_PLAIN)
  @Path("/{id}")
  public Response getAllBooks(@PathParam("id") String bookId,Book bookData){
    return Response.status(200)
		   .entity("Saved :- "+bookData.getId()).build();
  }
	
}



Result :-

JAX-RS @Consumes

Example 3 :-

In this example of JAX-RS @Consumes, we will hit this URL <base URL>/books to save the book resource sent in XML format.

import javax.ws.rs.Consumes;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("books")
public class ConsumesAnnotationExample {

  @PUT
  @Consumes(MediaType.APPLICATION_XML)
  @Produces(MediaType.TEXT_PLAIN)
  @Path("/{id}")
  public Response getAllBooks(@PathParam("id") String bookId,Book bookData){
    return Response.status(200)
		   .entity("Saved :- "+bookData.getId()).build();
  }
	
}



Result :-

JAX-RS @Consumes

References :-

  1. @Consumes Java Docs
  2. @Produces Java Docs

That’s all for JAX-RS @Consumes with Example. If you liked it, please share your thoughts in comments section and share it with others too.