001/*
002 * Copyright 2002-2017 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.messaging.converter;
018
019import org.springframework.lang.Nullable;
020import org.springframework.messaging.Message;
021import org.springframework.messaging.MessageHeaders;
022
023/**
024 * A converter to turn the payload of a {@link Message} from serialized form to a typed
025 * Object and vice versa. The {@link MessageHeaders#CONTENT_TYPE} message header may be
026 * used to specify the media type of the message content.
027 *
028 * @author Mark Fisher
029 * @author Rossen Stoyanchev
030 * @since 4.0
031 */
032public interface MessageConverter {
033
034        /**
035         * Convert the payload of a {@link Message} from a serialized form to a typed Object
036         * of the specified target class. The {@link MessageHeaders#CONTENT_TYPE} header
037         * should indicate the MIME type to convert from.
038         * <p>If the converter does not support the specified media type or cannot perform
039         * the conversion, it should return {@code null}.
040         * @param message the input message
041         * @param targetClass the target class for the conversion
042         * @return the result of the conversion, or {@code null} if the converter cannot
043         * perform the conversion
044         */
045        @Nullable
046        Object fromMessage(Message<?> message, Class<?> targetClass);
047
048        /**
049         * Create a {@link Message} whose payload is the result of converting the given
050         * payload Object to serialized form. The optional {@link MessageHeaders} parameter
051         * may contain a {@link MessageHeaders#CONTENT_TYPE} header to specify the target
052         * media type for the conversion and it may contain additional headers to be added
053         * to the message.
054         * <p>If the converter does not support the specified media type or cannot perform
055         * the conversion, it should return {@code null}.
056         * @param payload the Object to convert
057         * @param headers optional headers for the message (may be {@code null})
058         * @return the new message, or {@code null} if the converter does not support the
059         * Object type or the target media type
060         */
061        @Nullable
062        Message<?> toMessage(Object payload, @Nullable MessageHeaders headers);
063
064}