Class AbstractJackson2Encoder

java.lang.Object
org.springframework.http.codec.json.Jackson2CodecSupport
org.springframework.http.codec.json.AbstractJackson2Encoder
All Implemented Interfaces:
org.springframework.core.codec.Encoder<Object>, HttpMessageEncoder<Object>
Direct Known Subclasses:
Jackson2CborEncoder, Jackson2JsonEncoder, Jackson2SmileEncoder

public abstract class AbstractJackson2Encoder extends Jackson2CodecSupport implements HttpMessageEncoder<Object>
Base class providing support methods for Jackson 2.x encoding. For non-streaming use cases, Flux elements are collected into a List before serialization for performance reasons.
Since:
5.0
Author:
Sebastien Deleuze, Arjen Poutsma
  • Constructor Details

    • AbstractJackson2Encoder

      protected AbstractJackson2Encoder(ObjectMapper mapper, org.springframework.util.MimeType... mimeTypes)
      Constructor with a Jackson ObjectMapper to use.
  • Method Details

    • setStreamingMediaTypes

      public void setStreamingMediaTypes(List<MediaType> mediaTypes)
      Configure "streaming" media types for which flushing should be performed automatically vs at the end of the stream.
    • canEncode

      public boolean canEncode(org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType)
      Specified by:
      canEncode in interface org.springframework.core.codec.Encoder<Object>
    • encode

      public reactor.core.publisher.Flux<org.springframework.core.io.buffer.DataBuffer> encode(Publisher<?> inputStream, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType elementType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)
      Specified by:
      encode in interface org.springframework.core.codec.Encoder<Object>
    • encodeValue

      public org.springframework.core.io.buffer.DataBuffer encodeValue(Object value, org.springframework.core.io.buffer.DataBufferFactory bufferFactory, org.springframework.core.ResolvableType valueType, @Nullable org.springframework.util.MimeType mimeType, @Nullable Map<String,Object> hints)
      Specified by:
      encodeValue in interface org.springframework.core.codec.Encoder<Object>
    • customizeWriter

      protected ObjectWriter customizeWriter(ObjectWriter writer, @Nullable org.springframework.util.MimeType mimeType, org.springframework.core.ResolvableType elementType, @Nullable Map<String,Object> hints)
      Subclasses can use this method to customize the ObjectWriter used for writing values.
      Parameters:
      writer - the writer instance to customize
      mimeType - the selected MIME type
      elementType - the type of element values to write
      hints - a map with serialization hints; the Reactor Context, when available, may be accessed under the key ContextView.class.getName()
      Returns:
      the customized ObjectWriter to use
    • getStreamingMediaTypeSeparator

      @Nullable protected byte[] getStreamingMediaTypeSeparator(@Nullable org.springframework.util.MimeType mimeType)
      Return the separator to use for the given mime type.

      By default, this method returns new line "\n" if the given mime type is one of the configured streaming mime types.

      Since:
      5.3
    • getJsonEncoding

      protected JsonEncoding getJsonEncoding(@Nullable org.springframework.util.MimeType mimeType)
      Determine the JSON encoding to use for the given mime type.
      Parameters:
      mimeType - the mime type as requested by the caller
      Returns:
      the JSON encoding to use (never null)
      Since:
      5.0.5
    • getEncodableMimeTypes

      public List<org.springframework.util.MimeType> getEncodableMimeTypes()
      Specified by:
      getEncodableMimeTypes in interface org.springframework.core.codec.Encoder<Object>
    • getEncodableMimeTypes

      public List<org.springframework.util.MimeType> getEncodableMimeTypes(org.springframework.core.ResolvableType elementType)
      Specified by:
      getEncodableMimeTypes in interface org.springframework.core.codec.Encoder<Object>
    • getStreamingMediaTypes

      public List<MediaType> getStreamingMediaTypes()
      Description copied from interface: HttpMessageEncoder
      Return "streaming" media types for which flushing should be performed automatically vs at the end of the input stream.
      Specified by:
      getStreamingMediaTypes in interface HttpMessageEncoder<Object>
    • getEncodeHints

      public Map<String,Object> getEncodeHints(@Nullable org.springframework.core.ResolvableType actualType, org.springframework.core.ResolvableType elementType, @Nullable MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response)
      Description copied from interface: HttpMessageEncoder
      Get decoding hints based on the server request or annotations on the target controller method parameter.
      Specified by:
      getEncodeHints in interface HttpMessageEncoder<Object>
      Parameters:
      actualType - the actual source type to encode, possibly a reactive wrapper and sourced from MethodParameter, i.e. providing access to method annotations.
      elementType - the element type within Flux/Mono that we're trying to encode.
      request - the current request
      response - the current response
      Returns:
      a Map with hints, possibly empty
    • getAnnotation

      protected <A extends Annotation> A getAnnotation(org.springframework.core.MethodParameter parameter, Class<A> annotType)
      Specified by:
      getAnnotation in class Jackson2CodecSupport