org.springframework.messaging.converter

Class AbstractMessageConverter

  • java.lang.Object
    • org.springframework.messaging.converter.AbstractMessageConverter
    • Field Detail

      • logger

        protected final Log logger
    • Constructor Detail

      • AbstractMessageConverter

        protected AbstractMessageConverter(MimeType supportedMimeType)
        Construct an AbstractMessageConverter supporting a single MIME type.
        Parameters:
        supportedMimeType - the supported MIME type
      • AbstractMessageConverter

        protected AbstractMessageConverter(java.util.Collection<MimeType> supportedMimeTypes)
        Construct an AbstractMessageConverter supporting multiple MIME types.
        Parameters:
        supportedMimeTypes - the supported MIME types
    • Method Detail

      • getSupportedMimeTypes

        public java.util.List<MimeType> getSupportedMimeTypes()
        Return the supported MIME types.
      • setContentTypeResolver

        public void setContentTypeResolver(@Nullable
                                           ContentTypeResolver resolver)
        Configure the ContentTypeResolver to use to resolve the content type of an input message.

        Note that if no resolver is configured, then strictContentTypeMatch should be left as false (the default) or otherwise this converter will ignore all messages.

        By default, a DefaultContentTypeResolver instance is used.

      • setStrictContentTypeMatch

        public void setStrictContentTypeMatch(boolean strictContentTypeMatch)
        Whether this converter should convert messages for which no content type could be resolved through the configured ContentTypeResolver.

        A converter can configured to be strict only when a contentTypeResolver is configured and the list of supportedMimeTypes is not be empty.

        When this flag is set to true, supportsMimeType(MessageHeaders) will return false if the contentTypeResolver is not defined or if no content-type header is present.

      • isStrictContentTypeMatch

        public boolean isStrictContentTypeMatch()
        Whether content type resolution must produce a value that matches one of the supported MIME types.
      • setSerializedPayloadClass

        public void setSerializedPayloadClass(java.lang.Class<?> payloadClass)
        Configure the preferred serialization class to use (byte[] or String) when converting an Object payload to a Message.

        The default value is byte[].

        Parameters:
        payloadClass - either byte[] or String
      • getSerializedPayloadClass

        public java.lang.Class<?> getSerializedPayloadClass()
        Return the configured preferred serialization payload class.
      • getDefaultContentType

        @Nullable
        protected MimeType getDefaultContentType(java.lang.Object payload)
        Returns the default content type for the payload. Called when toMessage(Object, MessageHeaders) is invoked without message headers or without a content type header.

        By default, this returns the first element of the supportedMimeTypes, if any. Can be overridden in sub-classes.

        Parameters:
        payload - the payload being converted to message
        Returns:
        the content type, or null if not known
      • fromMessage

        @Nullable
        public final java.lang.Object fromMessage(Message<?> message,
                                                            java.lang.Class<?> targetClass)
        Description copied from interface: MessageConverter
        Convert the payload of a Message from a serialized form to a typed Object of the specified target class. The MessageHeaders.CONTENT_TYPE header should indicate the MIME type to convert from.

        If the converter does not support the specified media type or cannot perform the conversion, it should return null.

        Specified by:
        fromMessage in interface MessageConverter
        Parameters:
        message - the input message
        targetClass - the target class for the conversion
        Returns:
        the result of the conversion, or null if the converter cannot perform the conversion
      • canConvertFrom

        protected boolean canConvertFrom(Message<?> message,
                                         java.lang.Class<?> targetClass)
      • toMessage

        @Nullable
        public final Message<?> toMessage(java.lang.Object payload,
                                                    @Nullable
                                                    MessageHeaders headers)
        Description copied from interface: MessageConverter
        Create a Message whose payload is the result of converting the given payload Object to serialized form. The optional MessageHeaders parameter may contain a MessageHeaders.CONTENT_TYPE header to specify the target media type for the conversion and it may contain additional headers to be added to the message.

        If the converter does not support the specified media type or cannot perform the conversion, it should return null.

        Specified by:
        toMessage in interface MessageConverter
        Parameters:
        payload - the Object to convert
        headers - optional headers for the message (may be null)
        Returns:
        the new message, or null if the converter does not support the Object type or the target media type
      • supports

        protected abstract boolean supports(java.lang.Class<?> clazz)
        Whether the given class is supported by this converter.
        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)
        Convert the message payload from serialized form to an Object.
        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
        Since:
        4.2
      • convertToInternal

        @Nullable
        protected java.lang.Object convertToInternal(java.lang.Object payload,
                                                               @Nullable
                                                               MessageHeaders headers,
                                                               @Nullable
                                                               java.lang.Object conversionHint)
        Convert the payload object to serialized form.
        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
        Since:
        4.2