Method Return Types
The return type can be any serializable POJO as defined in {@doc PojoCategories}.
It can also be void if the method is not sending any output (e.g. a request redirect) or is
setting the output using the {@link oajr.RestResponse#setOutput(Object)} method.
@RestMethod(name=GET)
public String doGet() {
return "Hello World!";
}
Out-of-the-box, besides POJOs, the following return types are handled as special cases:
- {@link java.io.InputStream}
The contents are simply piped to the output stream returned by
{@link oajr.RestResponse#getNegotiatedOutputStream()}.
Note that you should call {@link oajr.RestResponse#setContentType(String)} to set
the Content-Type header if you use this object type.
- {@link java.io.Reader}
The contents are simply piped to the output stream returned by
{@link oajr.RestResponse#getNegotiatedWriter()}.
Note that you should call {@link oajr.RestResponse#setContentType(String)} to set
the Content-Type header if you use this object type.
- {@link oaj.Streamable}
Interface that identifies that an object can be serialized directly to an output stream.
- {@link oaj.Writable}
Interface that identifies that an object can be serialized directly to a writer.
- {@link oaj.utils.ZipFileList}
Special interface for sending zip files as responses.
This is controlled through the following extensible API:
- {@link oajr.ResponseHandler}
- {@link oajr.reshandlers.DefaultHandler}
- {@link oajr.reshandlers.InputStreamHandler}
- {@link oajr.reshandlers.ReaderHandler}
REST Java methods can generate output in any of the following ways:
-
By returning a serializable POJO, or any of the following:
{@link java.io.Reader}, {@link java.io.InputStream}, {@link oaj.Streamable},
{@link oaj.Writable}
-
By calling {@link oajr.RestResponse#setOutput(Object)} with any of the types above.
-
By accessing the {@link java.io.Writer} directly by calling
{@link oajr.RestResponse#getNegotiatedWriter()} and writing the output yourself.
// Equivalent method 1
@RestMethod(name=GET, path="/example1/{personId}")
public Person doGet1(@Path("personId") UUID personId) {
Person p = getPersonById(personId);
return p;
}
// Equivalent method 2
@RestMethod(name=GET, path="/example2/{personId}")
public void doGet2(RestResponse res, @Path("personId") UUID personId) {
Person p = getPersonById(personId);
res.setOutput(p);
}
// (Sorta) Equivalent method 3
// (Ignores any converters or method-level properties)
@RestMethod(name=GET, path="/example3/{personId}")
public void doGet3(RestRequest req, RestResponse res, @Path("personId") UUID personId) {
Person p = getPersonById(personId);
String accept = req.getHeader("Accept", "text/json");
WriterSerializer s = res.getSerializerGroup().getWriterSerializer(accept);
res.setContentType(s.getResponseContentType());
s.serialize(p, res.getNegotiatedWriter());
}
- {@link oajr.RestContext#REST_responseHandlers} - For configuring custom response handlers.