Java Method Return Types
The return type of the Java method can be any serializable POJO as defined in POJO Categories.
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 RestResponse.setContent(Object) method.
Example
@RestGet
public String doGet() {
return "Hello World!";
}
In addition to POJOs, the following return types are also supported:
Parameters based on class types:
Direct streams:
Apache HttpComponent beans:
Standard HTTP response beans:
Other:
Annotated parameters (either on the parameter or parameter type):
REST Java methods can also generate a response via the following:
- By calling RestResponse.setContent(Object) with any of the types above.
- By accessing the Writer directly by calling RestResponse.getNegotiatedWriter() and writing the output yourself.
Example
// Equivalent method 1
@RestGet("/example1/{personId}")
public Person doGet1(@Path("personId") UUID personId) {
Person person = getPersonById(personId);
return person;
}
// Equivalent method 2
@RestGet("/example2/{personId}")
public void doGet2(RestResponse res, @Path("personId") UUID personId) {
Person person = getPersonById(personId);
res.setContent(person);
}
Additional parameter types can be defined via the annotation Rest.responseProcessors() or by calling responseProcessors().
Example
@Rest(
responseProcessors={ MyResponseProcessor.class } // Option #1 - Via annotation
)
public class MyResource extends BasicRestObject {
// Option #2 - Programmatically
@RestInit
public void init(RestContext.Builder builder) {
builder.responseProcessors(MyResponseProcessor.class);
}
}