RequestEchoResource

The RequestEchoResource class shows how existing complex POJOs can be serialized to a variety of content types. The example simply takes the incoming HttpServletRequest object and serializes it.

It provides examples of the following:

The class is shown below:

RequestEchoResource.java

/** * Sample REST resource for echoing HttpServletRequests back to the browser */ @RestResource( path="/echo", title="Request echo service", description="Echos the current HttpServletRequest object back to the browser.", htmldoc=@HtmlDoc( widgets={ ContentTypeMenuItem.class, StyleMenuItem.class }, navlinks={ "up: request:/..", "options: servlet:/?method=OPTIONS", "$W{ContentTypeMenuItem}", "$W{StyleMenuItem}", "source: $C{Source/gitHub}/org/apache/juneau/examples/rest/$R{servletClassSimple}.java" }, aside={ "<div style='max-width:400px;min-width:200px' class='text'>", " <p>Shows how even arbitrary POJOs such as <code>HttpServletRequest</code> can be serialized by the framework.</p>", " <p>Also shows how to specify serializer properties, filters, and swaps at the servlet level to control how POJOs are serialized.</p>", " <p>Also provides an example of how to use the Traversable and Queryable APIs.</p>", "</div>" } ), properties={ @Property(name=BEANTRAVERSE_maxDepth, value="5"), @Property(name=BEANTRAVERSE_detectRecursions, value="true") }, beanFilters={ // Interpret these as their parent classes, not subclasses HttpServletRequest.class, HttpSession.class, ServletContext.class, }, pojoSwaps={ // Add a special filter for Enumerations EnumerationSwap.class } ) public class RequestEchoResource extends BasicRestServlet { /** GET request handler */ @RestMethod(name=GET, path="/*", converters={Queryable.class,Traversable.class}) public HttpServletRequest doGet(RestRequest req, RestResponse res, ObjectMap properties) { // Set the HtmlDocSerializer title programmatically. res.setPageTitle(req.getPathInfo()); // Just echo the request back as the response. return req; } }

Again, there's a lot going on here that's new that requires some explanation.
The HttpServletRequest object is not a tree-shaped POJO model.
Instead, it contains lots of loops that can cause stack overflow errors if you were to try to serialize it as-is.
Also, you want to look only at the properties defined on the HttpServletRequest class, not implementation-specific (i.e. WAS or Jetty) fields which can get messy.

The {@link oajr.annotation.RestResource#properties() @RestResource(properties)}, {@link oajr.annotation.RestResource#beanFilters() @RestResopurce(beanFilters)}, and {@link oajr.annotation.RestResource#pojoSwaps() @RestResopurce(pojoSwaps)} annotations are used to set behavior properties on the resource's underlying bean context, serializers, and parsers.
You're using them here to modify the behavior of serialization for all content types.
The annotations are functionally equivalent to using the {@link oajr.RestContextBuilder} class, as follows:

Hypothetical RequestEchoResource.createSerializers() method

/** Override the default rest serializers to add some transforms through an INIT hook*/ @RestHook(INIT) public void init(RestContextBuilder builder) throws Exception { // Add bean filters for the HttpServletRequest, HttpSession, and ServletContext objects // so that you don't show vendor-specific properties on subclasses. // Add Enumeration POJO swap to be able to render the contents of Enumeration properties. // The max depth and detect recursion options prevent any possible runaway serializations. // This shouldn't happen, but future JEE APIs may introduce deep hierarchies or loops. builder .beanFilters(HttpServletRequest.class, HttpSession.class, ServletContext.class) .pojoSwaps(EnumerationSwap.class) .setProperty(SERIALIZER_maxDepth, 5) .setProperty(SERIALIZER_detectRecursions, true) .pageLinks("{...}"); }

Note how the annotations generally require fewer lines of code.

Pointing a browser to the resource shows the following:

http://localhost:10000/echo

This gives you an idea of what kinds of POJO models can be serialized, since you are serializing a regular old HttpServletRequest object.