Serializers

REST resources use the {@link oaj.serializer.Serializer} API for defining serializers for serializing response POJOs.

The servlet will pick which serializer to use by matching the request Accept header with the media types defined through the {@link oaj.serializer.Serializer#getMediaTypeRanges()} method.

Serializers can be associated with REST servlets in the following ways:

The following are all equivalent ways of defining serializers used by a resource:

// Option #1 - Defined via annotation. @Rest(serializers={JsonSerializer.class, XmlSerializer.class}) public class MyResource { // Option #2 - Defined via builder passed in through resource constructor. public MyResource(RestContextBuilder builder) throws Exception { // Using method on builder. builder.serializers(JsonSerializer.class, XmlSerializer.class); // Same, but use pre-instantiated parsers. builder.serializers(JsonSerializer.DEFAULT, XmlSerializer.DEFAULT); // Same, but using property. builder.set(REST_serializers, JsonSerializer.class, XmlSerializer.class); } // Option #3 - Defined via builder passed in through init method. @RestHook(INIT) public void init(RestContextBuilder builder) throws Exception { builder.serializers(JsonSerializer.class, XmlSerializer.class); } // Override at the method level. @RestMethod(serializers={HtmlSerializer.class}) public MyPojo myMethod() { // Return a POJO to be serialized. return new MyPojo(); } }