45.2. Returning plain Java constructs
In many cases a resource class can return a standard Java type, a JAXB object, or any object for which the application has an entity provider. In these cases the runtime determines the MIME type information using the Java class of the object being returned. The runtime also determines the appropriate HTTP return code to send to the consumer.
Resource methods can return
voidor any Java type for which an entity writer is provided. By default, the runtime has providers for the following:
- the Java primitives
Numberrepresentations of the Java primitives
- JAXB objects
The runtime determines the MIME type of the returned entity by first checking the resource method and resource class for a
@Producesannotation. If it finds one, it uses the MIME type specified in the annotation. If it does not find one specified by the resource implementation, it relies on the entity providers to determine the proper MIME type.
By default the runtime assign MIME types as follows:
- Java primitives and their
Numberrepresentations are assigned a MIME type of
- JAXB objects are assigned a MIME type of
Applications can use other mappings by implementing custom entity providers as described in the section called “Custom writers”.
When resource methods return plain Java constructs, the runtime automatically sets the response's status code if the resource method completes without throwing an exception. The status code is set as follows:
204(No Content)—the resource method's return type is
204(No Content)—the value of the returned entity is
200(OK)—the value of the returned entity is not
If an exception is thrown before the resource method completes the return status code is set as described in Chapter 47, Handling Exceptions.