/*
 * Copyright 2002-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.http.converter;

import java.io.IOException;
import java.util.List;

import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;

Strategy interface for converting from and to HTTP requests and responses.
Author:Arjen Poutsma, Juergen Hoeller
Type parameters:
  • <T> – the converted object type
Since:3.0
/** * Strategy interface for converting from and to HTTP requests and responses. * * @author Arjen Poutsma * @author Juergen Hoeller * @since 3.0 * @param <T> the converted object type */
public interface HttpMessageConverter<T> {
Indicates whether the given class can be read by this converter.
Params:
  • clazz – the class to test for readability
  • mediaType – the media type to read (can be null if not specified); typically the value of a Content-Type header.
Returns:true if readable; false otherwise
/** * Indicates whether the given class can be read by this converter. * @param clazz the class to test for readability * @param mediaType the media type to read (can be {@code null} if not specified); * typically the value of a {@code Content-Type} header. * @return {@code true} if readable; {@code false} otherwise */
boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);
Indicates whether the given class can be written by this converter.
Params:
  • clazz – the class to test for writability
  • mediaType – the media type to write (can be null if not specified); typically the value of an Accept header.
Returns:true if writable; false otherwise
/** * Indicates whether the given class can be written by this converter. * @param clazz the class to test for writability * @param mediaType the media type to write (can be {@code null} if not specified); * typically the value of an {@code Accept} header. * @return {@code true} if writable; {@code false} otherwise */
boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);
Return the list of MediaType objects supported by this converter.
Returns:the list of supported media types, potentially an immutable copy
/** * Return the list of {@link MediaType} objects supported by this converter. * @return the list of supported media types, potentially an immutable copy */
List<MediaType> getSupportedMediaTypes();
Read an object of the given type from the given input message, and returns it.
Params:
  • clazz – the type of object to return. This type must have previously been passed to the canRead method of this interface, which must have returned true.
  • inputMessage – the HTTP input message to read from
Throws:
Returns:the converted object
/** * Read an object of the given type from the given input message, and returns it. * @param clazz the type of object to return. This type must have previously been passed to the * {@link #canRead canRead} method of this interface, which must have returned {@code true}. * @param inputMessage the HTTP input message to read from * @return the converted object * @throws IOException in case of I/O errors * @throws HttpMessageNotReadableException in case of conversion errors */
T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException;
Write an given object to the given output message.
Params:
  • t – the object to write to the output message. The type of this object must have previously been passed to the canWrite method of this interface, which must have returned true.
  • contentType – the content type to use when writing. May be null to indicate that the default content type of the converter must be used. If not null, this media type must have previously been passed to the canWrite method of this interface, which must have returned true.
  • outputMessage – the message to write to
Throws:
/** * Write an given object to the given output message. * @param t the object to write to the output message. The type of this object must have previously been * passed to the {@link #canWrite canWrite} method of this interface, which must have returned {@code true}. * @param contentType the content type to use when writing. May be {@code null} to indicate that the * default content type of the converter must be used. If not {@code null}, this media type must have * previously been passed to the {@link #canWrite canWrite} method of this interface, which must have * returned {@code true}. * @param outputMessage the message to write to * @throws IOException in case of I/O errors * @throws HttpMessageNotWritableException in case of conversion errors */
void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException; }