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 * An extended {@link MessageConverter} SPI with conversion hint support.
025 *
026 * <p>In case of a conversion hint being provided, the framework will call
027 * these extended methods if a converter implements this interface, instead
028 * of calling the regular {@code fromMessage} / {@code toMessage} variants.
029 *
030 * @author Juergen Hoeller
031 * @since 4.2.1
032 */
033public interface SmartMessageConverter extends MessageConverter {
034
035        /**
036         * A variant of {@link #fromMessage(Message, Class)} which takes an extra
037         * conversion context as an argument, allowing to take e.g. annotations
038         * on a payload parameter into account.
039         * @param message the input message
040         * @param targetClass the target class for the conversion
041         * @param conversionHint an extra object passed to the {@link MessageConverter},
042         * e.g. the associated {@code MethodParameter} (may be {@code null}}
043         * @return the result of the conversion, or {@code null} if the converter cannot
044         * perform the conversion
045         * @see #fromMessage(Message, Class)
046         */
047        @Nullable
048        Object fromMessage(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint);
049
050        /**
051         * A variant of {@link #toMessage(Object, MessageHeaders)} which takes an extra
052         * conversion context as an argument, allowing to take e.g. annotations
053         * on a return type into account.
054         * @param payload the Object to convert
055         * @param headers optional headers for the message (may be {@code null})
056         * @param conversionHint an extra object passed to the {@link MessageConverter},
057         * e.g. the associated {@code MethodParameter} (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         * @see #toMessage(Object, MessageHeaders)
061         */
062        @Nullable
063        Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint);
064
065}