Skip to main content

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:

InputStream Reader

Apache HttpComponent beans:

HttpEntity HttpResource HttpResponse

Standard HTTP response beans:

Accepted AlreadyReported BadRequest Conflict Continue Created EarlyHints ExpectationFailed FailedDependency Forbidden Found Gone HttpVersionNotSupported IMUsed InsufficientStorage InternalServerError LengthRequired Locked LoopDetected MethodNotAllowed MisdirectedRequest MovedPermanently MultipleChoices MultiStatus NetworkAuthenticationRequired NoContent NonAuthoritiveInformation NotAcceptable NotExtended NotFound NotImplemented NotModified Ok PartialContent PayloadTooLarge PermanentRedirect PreconditionFailed PreconditionRequired Processing RangeNotSatisfiable RequestHeaderFieldsTooLarge ResetContent SeeOther ServiceUnavailable SwitchingProtocols TemporaryRedirect TooManyRequests Unauthorized UnavailableForLegalReasons UnprocessableEntity UnsupportedMediaType UpgradeRequired UriTooLong UseProxy VariantAlsoNegotiates

Other:

ChildResourceDescriptions ResourceDescriptions SeeOtherRoot Throwable

Annotated parameters (either on the parameter or parameter type):

Response

REST Java methods can also generate a response via the following:

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);
}
}