ContentNegotiationServiceXRoDomainType
Handles content negotiation for accept headers requiring application/json
or application/xml
and specifying an x-ro-domain-type; will delegate to any available ContentMappingService s to (try to) map the result object into the required representation if possible.
In the accept header the profile is also checked dependent on the resource being invoked; either profile="urn:org.restfulobjects:repr-types/object"
for an object representation, or profile="profile=urn:org.restfulobjects:repr-types/action-result"
for an action result.
If the accept header specifies application/xml
then the service additionally verifies that the (mapped) domain object’s runtime type is annotated with the JAXB jakarta.xml.bind.annotation.XmlRootElement annotation so that RestEasy is able to unambiguously serialize it.
API
class ContentNegotiationServiceXRoDomainType {
public static final String X_RO_DOMAIN_TYPE;
Response.ResponseBuilder buildResponse(IResourceContext resourceContext, ManagedObject objectAdapter) (1)
Response.ResponseBuilder buildResponse(IResourceContext resourceContext, ObjectAndActionInvocation objectAndActionInvocation) (2)
}
1 | buildResponse(IResourceContext, ManagedObject)
search for an accept header in form |
2 | buildResponse(IResourceContext, ObjectAndActionInvocation)
search for an accept header in form |
Members
Usage Notes
The value of the x-ro-domain-type
parameter corresponds to the DTO to be mapped into by the ContentMappingService.
If the DTO is annotated with JAXB, then also note that the runtime type must be annotated with the JAXB javax.xml.bind.annotation.XmlRootElement
so that RestEasy is able to unambiguously serialize it.
Example
For example, consider a todo app that defines a ToDoItemDto
class, generated from an XSD and therefore JAXB annotated.
An implementation of ContentMappingService
that maps the application’s todoapp.dom.module.todoitem.ToDoItem
entities into todoapp.dto.module.todoitem.ToDoItemDto
classes.
A REST client can therefore request a DTO representation of an entity by invoking
http://localhost:8080/restful/objects/TODO/0
with an Accept
header of:
application/xml;profile=urn:org.restfulobjects:repr-types/object;x-ro-domain-type=todoapp.dto.module.todoitem.ToDoItemDto
will result in an XML serialization of that class:
while similarly hitting the same URL with an Accept
header of:
application/json;profile=urn:org.restfulobjects:repr-types/object;x-ro-domain-type=todoapp.dto.module.todoitem.ToDoItemDto
will result in the JSON serialization of that class: