uCosminexus Application Server, Web Service Development Guide

[Contents][Glossary][Index][Back][Next]

17.1.9 Media type declaration

You can respectively use the Consumes annotation and Produces annotation to specify the MIME media types supported by the Web resource. When these annotations are not used, all the media types are considered to be supported.

The Consumes annotation and Produces annotation can be used in:

The annotations used at the method level take precedence over the annotations used at the class level.

When two or more resource methods or sub-resource methods can process the same MIME media type, and if the request method designators, paths, or other information matches, the error (KDJJ10006-E) occurs. The system returns an HTTP response with 500 as the HTTP status code, in the root resource class and throws java.lang.RuntimeException, which can be handled by the exception mapping provider in the sub-resource class.

When the Content-Type header of an HTTP request does not match with any of the Consumes annotations, the (KDJJ10040-E) error occurs and the system throws javax.ws.rs.WebApplicationException, which has 415 as the HTTP status code and can be handled by the exception mapping provider.

If the HTTP Accept header of an HTTP response does not match with any of the Produces annotations, the (KDJJ10041-E) error occurs and the system throws javax.ws.rs.WebApplicationException, which has 406 as the HTTP status code and can be handled by the exception mapping provider.

An example of a media type declaration is as follows.

package com.sample.resources;
 
java.awt.image.RenderedImage
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
 
@Path("sample")
@Produces("image/jpeg")
public class ImageBasedResource {
 
@GET
  public RenderedImage getAsImage() {
  //implementation
  }
 
  @GET
  @Produces("text/html")
  public String getAsHtml() {
    //implementation 
  }
 
  @POST
  @Consumes("image/jpeg")
  public void addWidget(RenderedImage image) {
    //implementation 
  }
}

In this example, the resource method getAsImage() is called to process the HTTP GET request that requests the HTTP response of the MIME media type image/jpeg.

Furthermore, the resource method getAsHtml is called to process the HTTP GET request that requests the HTTP response of the MIME media type text/html.

Additionally, the resource method addImage is called to process the HTTPPOST request containing the entity body of the MIME media type image/jpeg.