Class JacksonJsonProvider

  • All Implemented Interfaces:
    com.fasterxml.jackson.core.Versioned, jakarta.ws.rs.ext.MessageBodyReader<Object>, jakarta.ws.rs.ext.MessageBodyWriter<Object>
    Direct Known Subclasses:
    JacksonXmlBindJsonProvider

    @Provider
    @Consumes("*/*")
    @Produces("*/*")
    public class JacksonJsonProvider
    extends ProviderBase<JacksonJsonProvider,​com.fasterxml.jackson.databind.ObjectMapper,​JsonEndpointConfig,​JsonMapperConfigurator>
    Basic implementation of Jakarta-RS abstractions (MessageBodyReader, MessageBodyWriter) needed for binding JSON ("application/json") content to and from Java Objects ("POJO"s).

    Actual data binding functionality is implemented by ObjectMapper: mapper to use can be configured in multiple ways:

    • By explicitly passing mapper to use in constructor
    • By explictly setting mapper to use by ProviderBase.setMapper(MAPPER)
    • By defining Jakarta-RS Provider that returns ObjectMappers.
    • By doing none of above, in which case a default mapper instance is constructed (and configured if configuration methods are called)
    The last method ("do nothing specific") is often good enough; explicit passing of Mapper is simple and explicit; and Provider-based method may make sense with Dependency Injection frameworks, or if Mapper has to be configured differently for different media types.

    Note that the default mapper instance will be automatically created if one of explicit configuration methods (like ProviderBase.configure(com.fasterxml.jackson.jakarta.rs.cfg.JakartaRSFeature, boolean)) is called: if so, Provider-based introspection is NOT used, but the resulting Mapper is used as configured.

    There is also (JacksonXmlBindJsonProvider) which is configured by default to use both Jackson and Jakarta XmlBin annotations for configuration (base class when used as-is defaults to using just Jackson annotations)

    Author:
    Tatu Saloranta
    • Field Detail

      • BASIC_ANNOTATIONS

        public static final Annotations[] BASIC_ANNOTATIONS
        Default annotation sets to use, if not explicitly defined during construction: only Jackson annotations are used for the base class. Sub-classes can use other settings.
      • _jsonpFunctionName

        protected String _jsonpFunctionName
        JSONP function name to use for automatic JSONP wrapping, if any; if null, no JSONP wrapping is done. Note that this is the default value that can be overridden on per-endpoint basis.
      • _providers

        @Context
        protected jakarta.ws.rs.ext.Providers _providers
        Injectable context object used to locate configured instance of ObjectMapper to use for actual serialization.
    • Constructor Detail

      • JacksonJsonProvider

        public JacksonJsonProvider()
        Default constructor, usually used when provider is automatically configured to be used with Jakarta-RS implementation.
      • JacksonJsonProvider

        public JacksonJsonProvider​(Annotations... annotationsToUse)
        Parameters:
        annotationsToUse - Annotation set(s) to use for configuring data binding
      • JacksonJsonProvider

        public JacksonJsonProvider​(com.fasterxml.jackson.databind.ObjectMapper mapper)
      • JacksonJsonProvider

        public JacksonJsonProvider​(com.fasterxml.jackson.databind.ObjectMapper mapper,
                                   Annotations[] annotationsToUse)
        Constructor to use when a custom mapper (usually components like serializer/deserializer factories that have been configured) is to be used.
        Parameters:
        annotationsToUse - Sets of annotations (Jackson, XmlBind) that provider should support