Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
Message converter Definition
We can rewrite configureMessageConverters () method (to replace the Spring Default converter created by MVC) or override extendMessageConverters() method (customize the default converter or add additional converters to the default converter) to customize HttpMessageConverter in a Java configuration.
HttpMessageConverter
The HttpMessageConverter excuse is defined as follows:
public interface HttpMessageConverter<T> { /** * 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. * @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 media types supported by this converter. The list may * not apply to every possible target element type and calls to this method * should typically be guarded via {@link #canWrite(Class, MediaType) * canWrite(clazz, null}. The list may also exclude MIME types supported * only for a specific class. Alternatively, use * {@link #getSupportedMediaTypes(Class)} for a more precise list. * @return the list of supported media types */ List<MediaType> getSupportedMediaTypes(); /** * Return the list of media types supported by this converter for the given * class. The list may differ from {@link #getSupportedMediaTypes()} if the * converter does not support the given Class or if it supports it only for * a subset of media types. * @param clazz the type of class to check * @return the list of media types supported for the given Class * @since 5.3.4 */ default List<MediaType> getSupportedMediaTypes(class <? > clazz) { return (canRead(clazz, null) || canWrite(clazz, null) ? getSupportedMediaTypes() : Collections.emptyList()); } /** * 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. * @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; }Copy the code
Custom message converter
Here is a simple message converter definition, coded as follows:
@Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder()
.indentOutput(true)
.dateFormat(new SimpleDateFormat("yyyy-MM-dd"))
.modulesToInstall(new ParameterNamesModule());
converters.add(new MappingJackson2HttpMessageConverter(builder.build()));
converters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build()));
}
}
Copy the code
In the previous example, Create a common configuration Jackson2ObjectMapperBuilder for MappingJackson2HttpMessageConverter and MappingJackson2XmlHttpMessageConverter, Enable indentation, custom date formatting, and Jackson module parameter name registration, which adds support for accessing parameter names (a feature added in Java8).
Configure FastJson as the default JSON parser
In fact, FastJSON is the default JSON parser for Spring MVC. The configuration is as follows:
<bean id="mappingJacksonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <! --class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">--> <property name="supportedMediaTypes"> <list> <value>text/html; charset=UTF-8</value> <value>application/json</value> <value>application/xml; charset=UTF-8</value> </list> </property> </bean>Copy the code
The resources
- Docs. Spring. IO/spring – fram…
- Blog.csdn.net/qq_37292960…