The @javax.ws.rs.Consumes annotation defines the internet media type(s) that the resource class methods can accept. You can define the @Consumes annotation either at the class level (which will get defaulted for all methods) or at the method level. The method-level annotations override the class-level annotations. The possible internet media types that a REST API can consume are as follows:
- application/atom+xml
- application/json
- application/octet-stream
- application/svg+xml
- application/xhtml+xml
- application/xml
- text/html
- text/plain
- text/xml
- multipart/form-data
- application/x-www-form-urlencoded
The following example illustrates how you can use the @Consumes attribute to designate a method in a class to consume a payload presented in the JSON media type. The binding provider will copy the JSON representation of an input message to the Department parameter of the createDepartment() method:
import javax.ws.rs.Consumes; import javax.ws.rs.core.MediaType; import javax.ws.rs.POST; @POST @Consumes(MediaType.APPLICATION_JSON) public void createDepartment(Department entity) { //Method implementation goes here... }
The javax.ws.rs.core.MediaType class defines constants for all the media types supported in JAX-RS. To learn more about the MediaType class, visit the API documentation available at http://docs.oracle.com/javaee/7/api/javax/ws/rs/core/MediaType.html.