Java Reference
In-Depth Information
object that accepts a string. Some implementations, such as Apache CFX, have extensions
for registering handlers to deal with problems like this. This is where you want to make
sure that the interfaces on your EJBs aren't driven by limitations within JAX-RS.
Using the @Produces annotation
The @javax.ws.rs.Produces annotation specifies the Multipurpose Internet Mail
Extensions (MIME) types the service can produce and return to the client. One or more
types can be returned to the client. The service should check the header values with the re-
quest to determine which one the client supports. The annotation is defined as follows:
@Inherited
@Target(value = {ElementType.TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Produces {
public String[] value() default {"*/*"};
}
The following example demonstrates the use of this annotation with a method that can re-
turn either XML or JSON depending on the client:
@GET
@Path("/getBid/{bidId}")
@Produces({"application/json","application/xml"})
public Bid getBid(@PathParam("bidId") long bidId) {
...
}
If you invoke this from the web browser, you'll see either XML or JSON. The JAX-RS
implementation can either generate XML using JAXB or generate JSON output.
Common MIME types, which might be produced by a method, include the following:
application/xml— XML
application/json— JSON-structured text
text/plain— Raw text
text/html— HTML output
application/octet-stream— Arbitrary binary data
This isn't an exhaustive list but a sampling of some of the common ones you'll use.
Search WWH ::




Custom Search