Java Reference
In-Depth Information
such as a requirement to return JSON structured data, drive the interface of the stateless
session bean. Consequently, the stateless session beans that should be exposed should have
relatively simple interfaces. As mentioned previously, these beans should primarily accept
primitives or strings and return simple output.
The JAX-RS annotations should be placed on the local interface. Placing them directly on
the stateless interface bean makes the implementation harder to read and also ties the bean
directly to JAX-RS. The convoluted code will be harder to maintain and to read. In the
future, it might be necessary to use the Adapter Pattern and split the bean off; therefore,
having a separate interface will simplify such an architectural change.
In situations where the interface of the EJB must be adapted to support RESTful web ser-
vices, the Adapter Pattern should be used. With the Adapter Pattern, the RESTful web ser-
vice is implemented as a POJO. CDI (context and dependency injection) can be used to
inject references to the EJB. CDI is covered in the next chapter. The adapter RESTful web
service translates the request and response messages into forms that are acceptable to EJBs
so that RESTful interface requirements don't infiltrate the business tier.
When using REST to expose an EJB, you should closely follow the semantics of REST.
The operations on the EJB must be appropriately mapped to the HTTP methods. A method
that removes a record should use an HTTP DELETE . In addition, an EJB method that re-
trieves data should use an HTTP GET . The principles of safety and idempotent methods
are essential to building a well-architected solution. If you've worked mostly with SOAP-
based web services, this can require a mental shift to adapt.
Above all, RESTful web services should be kept simple. RESTful web services are meant
to be lightweight. A simple litmus test should be whether you could invoke your web ser-
vices from the browser. If code is required to invoke the web service, then perhaps the ser-
vice would be better implemented using SOAP. RESTful web services aren't XML-cent-
ric—if you find your services making extensive use of JAXB to handle marshaling and
demarshaling of XML object graphs, then it's time to reconsider the design.
8.4. Choosing between SOAP and REST
This chapter discusses two different web service approaches: SOAP and REST. The first
decision that you must make when exposing services is which approach to take. There's no
Search WWH ::




Custom Search