{title:'Response Processors', created:'9.0.0'}

The REST Server API uses the concept of registered response processors for converting objects returned by REST methods or set through {@link oajr.RestResponse#setContent(Object)} into appropriate HTTP responses.

By default, REST resource classes are registered with the following response processors:

- {@link oajr.processor.HttpEntityProcessor}
- {@link oajr.processor.HttpResourceProcessor}
- {@link oajr.processor.HttpResponseProcessor}
- {@link oajr.processor.InputStreamProcessor}
- {@link oajr.processor.PlainTextPojoProcessor}
- {@link oajr.processor.ReaderProcessor}
- {@link oajr.processor.ResponseBeanProcessor}
- {@link oajr.processor.SerializedPojoProcessor}
- {@link oajr.processor.ThrowableProcessor}

Custom response processors can be associated with REST resources via the following:

- {@link oajr.annotation.Rest#responseProcessors}
- {@link oajr.RestContext.Builder#responseProcessors()}

Response processors can be used to process POJOs that cannot normally be handled through Juneau serializers, or because it's simply easier to define response processors for special cases.

The following example shows how to create a response processor to handle special Foo objects outside the normal Juneau architecture.

@Rest( path="/example", responseProcessors=FooProcessor.class ) public class Example extends BasicRestServlet implements BasicUniversalConfig { @RestGet("/") public Foo test1() { return new Foo("123"); } public static class FooProcessor implements ResponseProcessor { @Override public int process(RestOpSession opSession) { RestResponse res = opSession.getRestResponse(); Foo foo = res.getOutput(Foo.class); if (foo == null) return NEXT; // Let the next processor handle it. // Set some headers and body content. res.setHeader("Foo-ID", foo.getId()); res.getWriter().write("foo.id=" + foo.getId()); return FINISHED; // We handled it. } } }