|
| 1 | +/* |
| 2 | + * Copyright 2002-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.http.converter; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import org.springframework.core.ResolvableType; |
| 23 | +import org.springframework.http.HttpInputMessage; |
| 24 | +import org.springframework.http.HttpOutputMessage; |
| 25 | +import org.springframework.http.MediaType; |
| 26 | +import org.springframework.lang.Nullable; |
| 27 | + |
| 28 | +/** |
| 29 | + * A specialization of {@link HttpMessageConverter} that can convert an HTTP request |
| 30 | + * into a target object of a specified {@link ResolvableType} and a source object of |
| 31 | + * a specified {@link ResolvableType} into an HTTP response with optional hints. |
| 32 | + * |
| 33 | + * <p>It provides default methods for {@link HttpMessageConverter} in order to allow |
| 34 | + * subclasses to only have to implement the smart APIs. |
| 35 | + * |
| 36 | + * @author Sebastien Deleuze |
| 37 | + * @since 6.2 |
| 38 | + * @param <T> the converted object type |
| 39 | + */ |
| 40 | +public interface SmartHttpMessageConverter<T> extends HttpMessageConverter<T> { |
| 41 | + |
| 42 | + /** |
| 43 | + * Indicates whether the given type can be read by this converter. |
| 44 | + * This method should perform the same checks as |
| 45 | + * {@link HttpMessageConverter#canRead(Class, MediaType)} with additional ones |
| 46 | + * related to the generic type. |
| 47 | + * @param type the (potentially generic) type to test for readability. The |
| 48 | + * {@linkplain ResolvableType#getSource() type source} may be used for retrieving |
| 49 | + * additional information (the related method signature for example) when relevant. |
| 50 | + * @param mediaType the media type to read, can be {@code null} if not specified. |
| 51 | + * Typically, the value of a {@code Content-Type} header. |
| 52 | + * @return {@code true} if readable; {@code false} otherwise |
| 53 | + */ |
| 54 | + boolean canRead(ResolvableType type, @Nullable MediaType mediaType); |
| 55 | + |
| 56 | + @Override |
| 57 | + default boolean canRead(Class<?> clazz, @Nullable MediaType mediaType) { |
| 58 | + return canRead(ResolvableType.forClass(clazz), mediaType); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Read an object of the given type from the given input message, and returns it. |
| 63 | + * @param type the (potentially generic) type of object to return. This type must have |
| 64 | + * previously been passed to the {@link #canRead(ResolvableType, MediaType) canRead} |
| 65 | + * method of this interface, which must have returned {@code true}. The |
| 66 | + * {@linkplain ResolvableType#getSource() type source} may be used for retrieving |
| 67 | + * additional information (the related method signature for example) when relevant. |
| 68 | + * @param inputMessage the HTTP input message to read from |
| 69 | + * @param hints additional information about how to encode |
| 70 | + * @return the converted object |
| 71 | + * @throws IOException in case of I/O errors |
| 72 | + * @throws HttpMessageNotReadableException in case of conversion errors |
| 73 | + */ |
| 74 | + T read(ResolvableType type, HttpInputMessage inputMessage, @Nullable Map<String, Object> hints) |
| 75 | + throws IOException, HttpMessageNotReadableException; |
| 76 | + |
| 77 | + @Override |
| 78 | + default T read(Class<? extends T> clazz, HttpInputMessage inputMessage) |
| 79 | + throws IOException, HttpMessageNotReadableException { |
| 80 | + |
| 81 | + return read(ResolvableType.forClass(clazz), inputMessage, null); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Indicates whether the given class can be written by this converter. |
| 86 | + * <p>This method should perform the same checks as |
| 87 | + * {@link HttpMessageConverter#canWrite(Class, MediaType)} with additional ones |
| 88 | + * related to the generic type. |
| 89 | + * @param targetType the (potentially generic) target type to test for writability |
| 90 | + * (can be {@link ResolvableType#NONE} if not specified). The {@linkplain ResolvableType#getSource() type source} |
| 91 | + * may be used for retrieving additional information (the related method signature for example) when relevant. |
| 92 | + * @param valueClass the source object class to test for writability |
| 93 | + * @param mediaType the media type to write (can be {@code null} if not specified); |
| 94 | + * typically the value of an {@code Accept} header. |
| 95 | + * @return {@code true} if writable; {@code false} otherwise |
| 96 | + */ |
| 97 | + boolean canWrite(ResolvableType targetType, Class<?> valueClass, @Nullable MediaType mediaType); |
| 98 | + |
| 99 | + @Override |
| 100 | + default boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) { |
| 101 | + return canWrite(ResolvableType.forClass(clazz), clazz, mediaType); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Write a given object to the given output message. |
| 106 | + * @param t the object to write to the output message. The type of this object must |
| 107 | + * have previously been passed to the {@link #canWrite canWrite} method of this |
| 108 | + * interface, which must have returned {@code true}. |
| 109 | + * @param type the (potentially generic) type of object to write. This type must have |
| 110 | + * previously been passed to the {@link #canWrite canWrite} method of this interface, |
| 111 | + * which must have returned {@code true}. Can be {@link ResolvableType#NONE} if not specified. |
| 112 | + * The {@linkplain ResolvableType#getSource() type source} may be used for retrieving additional |
| 113 | + * information (the related method signature for example) when relevant. |
| 114 | + * @param contentType the content type to use when writing. May be {@code null} to |
| 115 | + * indicate that the default content type of the converter must be used. If not |
| 116 | + * {@code null}, this media type must have previously been passed to the |
| 117 | + * {@link #canWrite canWrite} method of this interface, which must have returned |
| 118 | + * {@code true}. |
| 119 | + * @param outputMessage the message to write to |
| 120 | + * @param hints additional information about how to encode |
| 121 | + * @throws IOException in case of I/O errors |
| 122 | + * @throws HttpMessageNotWritableException in case of conversion errors |
| 123 | + */ |
| 124 | + void write(T t, ResolvableType type, @Nullable MediaType contentType, HttpOutputMessage outputMessage, |
| 125 | + @Nullable Map<String, Object> hints) throws IOException, HttpMessageNotWritableException; |
| 126 | + |
| 127 | + @Override |
| 128 | + default void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage) |
| 129 | + throws IOException, HttpMessageNotWritableException { |
| 130 | + write(t, ResolvableType.forInstance(t), contentType, outputMessage, null); |
| 131 | + } |
| 132 | +} |
0 commit comments