Java Reference
In-Depth Information
Using the @Consumes annotation
The @javax.ws.rs.Consumes annotation is similar to the @Produces annotation.
The only difference is that it specifies the MIME type representations the service can ac-
cept. This annotation is used in situations where the method accepts different types of in-
put. The annotation is defined as follows:
@Inherited
@Target(value = {ElementType.TYPE, ElementType.METHOD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface Consumes {
public String[] value() default {"*/*"};
}
Consider the following example code demonstrating this annotation:
@POST @Path("/addBid")
@Consumes("application/xml")
public void addBid(Bid bid) {
...
}
This method accepts an XML representation of a Bid . The JAX-RS implementation will
use JAXB to convert the XML document into a com.actionbazaar.dto.Bid in-
stance. Note that a DTO used as a com.actionbazaar.persistence.Bid instance
has references to the Item and Bidder —you don't want an entire serialized object graph.
As mentioned earlier, this is only a sampling of the annotation capabilities provided by
JAX-RS. A full discussion of JAX-RS requires a separate book. A good book that covers
just REST and Java is Restlet in Action by Jerome Louvel (Manning, 2012).
In the next section we'll look at how EJB and RESTful web services can be used effect-
ively. Using REST effectively is a much larger topic.
8.3.6. Using EJB and REST web services effectively
Exposing stateless session beans via REST requires planning ahead and carefully crafting
the external interface. Unlike SOAP-based web services, RESTful web services are much
closer to the wire format. You must take care to avoid having client-related requirements,
Search WWH ::




Custom Search