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