Java Reference
In-Depth Information
NOTE
Apache Commons Codec and Logging are available at
http://commons.apache.org/
. This example
uses version 3.1 of the HTTP Commons Client, available at
http://hc.apache.org/
.
The REST service that presents three different representations of the same resource is shown
will automatically be routed to the appropriate method based on either its declared
Accept
content type or its preferences.
Example8-13.DifferentRepresentations.java
package com.soacookbook.rest.ex;
import java.io.File;
import javax.activation.MimetypesFileTypeMap;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Response;
/**
* If we put a @Produces annotation on the class level,
* that will be the value matched despite browser preferences
* in the Accept header.
*/
@Path("/duke")
@Produces("text/plain")
public class DifferentRepresentations {
private static final String IMG_PATH =
"C:\\programs/eclipse/workspace/restexamples/WebContent/
duke.gif";
@GET
@Produces("text/html")
public String doGetAsHtml() {
return "<html><h1>Html Duke</h1></html>";
}
//notice no override, as this method returns the default
@GET
public String doGetAsPlainText() {
return "Plain Duke";