org.springframework.messaging.converter

Class MappingJackson2MessageConverter

    • Constructor Detail

      • MappingJackson2MessageConverter

        public MappingJackson2MessageConverter()
        Construct a MappingJackson2MessageConverter supporting the application/json MIME type with UTF-8 character set.
      • MappingJackson2MessageConverter

        public MappingJackson2MessageConverter(MimeType... supportedMimeTypes)
        Construct a MappingJackson2MessageConverter supporting one or more custom MIME types.
        Parameters:
        supportedMimeTypes - the supported MIME types
        Since:
        4.1.5
    • Method Detail

      • setObjectMapper

        public void setObjectMapper(ObjectMapper objectMapper)
        Set the ObjectMapper for this converter. If not set, a default ObjectMapper is used.

        Setting a custom-configured ObjectMapper is one way to take further control of the JSON serialization process. For example, an extended SerializerFactory can be configured that provides custom serializers for specific types. The other option for refining the serialization process is to use Jackson's provided annotations on the types to be serialized, in which case a custom-configured ObjectMapper is unnecessary.

      • getObjectMapper

        public ObjectMapper getObjectMapper()
        Return the underlying ObjectMapper for this converter.
      • setPrettyPrint

        public void setPrettyPrint(boolean prettyPrint)
        Whether to use the DefaultPrettyPrinter when writing JSON. This is a shortcut for setting up an ObjectMapper as follows:
         ObjectMapper mapper = new ObjectMapper();
         mapper.configure(SerializationFeature.INDENT_OUTPUT, true);
         converter.setObjectMapper(mapper);
         
      • supports

        protected boolean supports(java.lang.Class<?> clazz)
        Description copied from class: AbstractMessageConverter
        Whether the given class is supported by this converter.
        Specified by:
        supports in class AbstractMessageConverter
        Parameters:
        clazz - the class to test for support
        Returns:
        true if supported; false otherwise
      • convertFromInternal

        @Nullable
        protected java.lang.Object convertFromInternal(Message<?> message,
                                                                 java.lang.Class<?> targetClass,
                                                                 @Nullable
                                                                 java.lang.Object conversionHint)
        Description copied from class: AbstractMessageConverter
        Convert the message payload from serialized form to an Object.
        Overrides:
        convertFromInternal in class AbstractMessageConverter
        Parameters:
        message - the input message
        targetClass - the target class for the conversion
        conversionHint - an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}
        Returns:
        the result of the conversion, or null if the converter cannot perform the conversion
      • convertToInternal

        @Nullable
        protected java.lang.Object convertToInternal(java.lang.Object payload,
                                                               @Nullable
                                                               MessageHeaders headers,
                                                               @Nullable
                                                               java.lang.Object conversionHint)
        Description copied from class: AbstractMessageConverter
        Convert the payload object to serialized form.
        Overrides:
        convertToInternal in class AbstractMessageConverter
        Parameters:
        payload - the Object to convert
        headers - optional headers for the message (may be null)
        conversionHint - an extra object passed to the MessageConverter, e.g. the associated MethodParameter (may be null}
        Returns:
        the resulting payload for the message, or null if the converter cannot perform the conversion
      • getSerializationView

        @Nullable
        protected java.lang.Class<?> getSerializationView(@Nullable
                                                                    java.lang.Object conversionHint)
        Determine a Jackson serialization view based on the given conversion hint.
        Parameters:
        conversionHint - the conversion hint Object as passed into the converter for the current conversion attempt
        Returns:
        the serialization view class, or null if none
        Since:
        4.2
      • getJsonEncoding

        protected JsonEncoding getJsonEncoding(@Nullable
                                               MimeType contentType)
        Determine the JSON encoding to use for the given content type.
        Parameters:
        contentType - the MIME type from the MessageHeaders, if any
        Returns:
        the JSON encoding to use (never null)