public interface ResponseHandler
The REST Server API uses the concept of registered response handlers for converting objects returned by REST
methods or set through RestResponse.setOutput(Object)
into appropriate HTTP responses.
Response handlers can be associated with REST resources via the following:
RestResource.responseHandlers()
RestContextBuilder.responseHandlers(Class...)
RestContextBuilder.responseHandlers(ResponseHandler...)
By default, REST resources are registered with the following response handlers:
DefaultHandler
- Serializes POJOs using the Juneau serializer API.
ReaderHandler
- Pipes the output of Readers
to the response writer
(RestResponse.getWriter()
).
InputStreamHandler
- Pipes the output of InputStreams
to the response output
stream (RestResponse.getOutputStream()
).
RedirectHandler
- Handles Redirect
objects.
WritableHandler
- Handles Writable
objects.
StreamableHandler
- Handles Streamable
objects.
Response handlers can be used to process POJOs that cannot normally be handled through Juneau serializers, or because it's simply easier to define response handlers for special cases.
The following example shows how to create a response handler to handle special Foo
objects outside the
normal Juneau architecture.
Modifier and Type | Method and Description |
---|---|
boolean |
handle(RestRequest req,
RestResponse res,
Object output)
Process this response if possible.
|
boolean handle(RestRequest req, RestResponse res, Object output) throws IOException, RestException
req
- The HTTP servlet request.res
- The HTTP servlet response;output
- The POJO returned by the REST method that now needs to be sent to the response.IOException
- If low-level exception occurred on output stream.
Results in a HttpServletResponse.SC_INTERNAL_SERVER_ERROR
error.RestException
- If some other exception occurred.
Can be used to provide an appropriate HTTP response code and message.Copyright © 2018 Apache. All rights reserved.