001/*
002 * Copyright 2002-2020 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.http.converter;
018
019import java.io.IOException;
020import java.util.List;
021
022import org.springframework.http.HttpInputMessage;
023import org.springframework.http.HttpOutputMessage;
024import org.springframework.http.MediaType;
025import org.springframework.lang.Nullable;
026
027/**
028 * Strategy interface for converting from and to HTTP requests and responses.
029 *
030 * @author Arjen Poutsma
031 * @author Juergen Hoeller
032 * @since 3.0
033 * @param <T> the converted object type
034 */
035public interface HttpMessageConverter<T> {
036
037        /**
038         * Indicates whether the given class can be read by this converter.
039         * @param clazz the class to test for readability
040         * @param mediaType the media type to read (can be {@code null} if not specified);
041         * typically the value of a {@code Content-Type} header.
042         * @return {@code true} if readable; {@code false} otherwise
043         */
044        boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);
045
046        /**
047         * Indicates whether the given class can be written by this converter.
048         * @param clazz the class to test for writability
049         * @param mediaType the media type to write (can be {@code null} if not specified);
050         * typically the value of an {@code Accept} header.
051         * @return {@code true} if writable; {@code false} otherwise
052         */
053        boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);
054
055        /**
056         * Return the list of {@link MediaType} objects supported by this converter.
057         * @return the list of supported media types, potentially an immutable copy
058         */
059        List<MediaType> getSupportedMediaTypes();
060
061        /**
062         * Read an object of the given type from the given input message, and returns it.
063         * @param clazz the type of object to return. This type must have previously been passed to the
064         * {@link #canRead canRead} method of this interface, which must have returned {@code true}.
065         * @param inputMessage the HTTP input message to read from
066         * @return the converted object
067         * @throws IOException in case of I/O errors
068         * @throws HttpMessageNotReadableException in case of conversion errors
069         */
070        T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
071                        throws IOException, HttpMessageNotReadableException;
072
073        /**
074         * Write an given object to the given output message.
075         * @param t the object to write to the output message. The type of this object must have previously been
076         * passed to the {@link #canWrite canWrite} method of this interface, which must have returned {@code true}.
077         * @param contentType the content type to use when writing. May be {@code null} to indicate that the
078         * default content type of the converter must be used. If not {@code null}, this media type must have
079         * previously been passed to the {@link #canWrite canWrite} method of this interface, which must have
080         * returned {@code true}.
081         * @param outputMessage the message to write to
082         * @throws IOException in case of I/O errors
083         * @throws HttpMessageNotWritableException in case of conversion errors
084         */
085        void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
086                        throws IOException, HttpMessageNotWritableException;
087
088}