Java Reference
In-Depth Information
Build and Run the Example Program
Perform the following steps:
1. Open a command prompt or shell terminal and change to the ex06_1 directory of the
workbook example code.
2. Make sure your PATH is set up to include both the JDK and Maven, as described in
Chapter 17 .
3. Perform the build and run the example by typing maven install .
Example ex06_2: Creating a Content Handler
For this example, we're going to create something entirely new. The Chapter 6 example of a
content handler is a reimplementation of JAXB support. It is suitable for that chapter because
it illustrates both the writing of a MessageBodyReader and a MessageBodyWriter and
demonstrates how the ContextResolver is used. For ex06_2 , though, we're going to keep
things simple.
In ex06_2 , we're going to rewrite ex06_1 to exchange Java objects between the client and
server instead of XML. Java objects, you ask? Isn't this REST? Well, there's no reason a
Java object can't be a valid representation of a resource! If you're exchanging Java objects,
you can still realize a lot of the advantages of REST and HTTP. You still can do content ne-
gotiation (described in Chapter 9 ) and HTTP caching (described in Chapter 11 ) .
The Content Handler Code
For our Java object content handler, we're going to write one class that is both a Mes-
sageBodyReader and a MessageBodyWriter :
src/main/java/com/restfully/shop/services/JavaMarshaller.java
@Provider
@Produces ( "application/example-java" )
@Consumes ( "application/x-java-serialized-object" )
public
public class
class JavaMarshaller
JavaMarshaller
implements
implements MessageBodyReader , MessageBodyWriter
{
The JavaMarshaller class is annotated with @Provider , @Produces , and @Consumes , as re-
quired by the specification. The media type used by the example to represent a Java object is
application/example-java : [ 22 ]
 
Search WWH ::




Custom Search