Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

JAX-RS: Developing RESTful Web Services in Java : Page 4

Learn how the JAX-RS API and annotations enable you to develop REST-based web services in Java.


advertisement

MIME Types Supported by JAX-RS for Request and Response

You use MIME media types to identify HTTP request entities and representations. A resource class can produce or consume any type of MIME. You use annotations to specify the MIME media type for request and response on a resource class or resource method. You use the annotation @Produces to specify the MIME type for the response (a representation that can be produced by a resource and sent back to the client) and @Consumes to specify the MIME type for the request (a representation of the specific content types that a resource can accept from an HTTP request entity, or evidence that it can consume the content sent by client).

To specify a simple text/plain MIME, you use @Produces ("text/plain") and @Consumes ("text/plain").



package com.rest.demo; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; @Path("/helloworld") public class Hello { @GET @Produces("text/plain") public String sayHello() { return "Hello, Welcome to the World of REST"; } }

If the MIME type is HTML, then you use "text/html" as the value for @Produces and @Consumes.

package com.demo; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; @Path("/myrest") public class DemoResource { public DemoResource() { } /** * Retrieves representation of an instance of com.demo.DemoResource * @return an instance of java.lang.String */ @GET @Produces("text/html") public String getHtml() { return ("Hello, This is demo for REST application"); } /** * PUT method for updating or creating an instance of DemoResource * @param content representation for the resource * @return an HTTP response with content of the updated or created resource. */ @PUT @Consumes("text/html") public void putHtml(String content) { } }

The following example demonstrates the use of multiple MIME media types by employing @Produces and @Consumes:

package com.demo; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.PUT; import javax.ws.rs.Produces; import javax.ws.rs.Consumes; @Path("/sayHello") @Produces("application/xml") public class SayHelloResource { @GET public String getXml() {...} @GET @Produces("text/html") public String getHtml() {...} @PUT @Consumes("application/xml") public void putXml(String content) {...} }

In the above example:

  • The getXml resource method will be called for GET requests that specify a MIME type of application/xml. It returns an object of string.
  • The getHtml resource method will be called for GET requests that specify a MIME type of text/html. It returns a string containing text/html.
  • The putXml resource method will be called for PUT requests that return an XML representation.

If the value of a resource method's @Produces annotation is not supported by the HTTP request, then the resource method will not be invoked. Similarly, if the value of a resource method's @Consumes annotation does not match the header, the resource method will not be invoked. If these annotations are not present, then any MIME type (*/*) is supported.

Providers

JAX-RS entity providers help in the mapping between entities and associated Java types. The two types of entity providers JAX-RS supports are:
  1. MessageBodyReader: a class that you use to map an HTTP request entity body to method parameters
  2. MessageBodyWriter: a class that you use to map the return value to the HTTP response entity body.

By default, MessageBodyReader and MessageBodyWriter automatically support the following standard types:

  • byte[]: All media types (*/*)
  • java.lang.String: All media types (*/*)
  • java.io.InputStream: All media types (*/*)
  • java.io.Reader: All media types (*/*)
  • java.io.File: All media types (*/*)
  • javax.activation.DataSource: All media types (*/*)
  • javax.xml.transform.Source: For XML Media types only (text/xml, application/xml, application/*+xml)
  • javax.xml.bind.JAXBElement and application-supplied JAXB classes: For XML Media types only (text/xml, application/xml, application/*+xml)
  • MultivaluedMap<String, String>: Form content (application/x-www-form-urlencoded)
  • StreamingOutput: All media types (*/*), only for MessageBodyWriter

If the application chooses not to use any of the standard types, a method can return an object of response, which is built using the ResponseBuilder class.

JAX-RS allows you to write custom mapping from/to a representation and an entity body. The classes that provide custom mapping are annotated with @Provider and they implement the MessageBodyReader or MessageBodyWriter classes.

An entity provider is a resource class annotated with @Provider that implements the JAX-RS API. You can use the @Provider annotation along with @Produces and @Consumes as demonstrated below:

.... ... @PUT @Consumes("application/stockquote+xml") public void createStock(Stock stock ) { ...... } ........ @Provider @Produces("application/stockquote+xml") public class StockProvider implements MessageBodyWriter<Stock> { ..... } @Provider @Consumes("application/stockquote+xml") public class StProvider implements MessageBodyReader<St> { ..... }

The value of the stock parameter will be mapped from the request entity body using the StProvider class. Similarly, when you need to map from the representation to the response entity body, you use the StockProvider class.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap