Spring 源碼解析|消息轉(zhuǎn)換
消息轉(zhuǎn)換器定義
我們可以通過重寫 configureMessageConverters()方法(替換Spring MVC創(chuàng)建的默認(rèn)轉(zhuǎn)換器)或重寫extendMessageConverters()方法(自定義默認(rèn)轉(zhuǎn)換器或向默認(rèn)轉(zhuǎn)換器添加其他轉(zhuǎn)換器)在Java配置中自定義 HttpMessageConverter。
HttpMessageConverter
消息轉(zhuǎn)換器 HttpMessageConverter 借口定義如下:
public interface HttpMessageConverter {
/**
* 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 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 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;
}
復(fù)制代碼 自定義消息轉(zhuǎn)換器
下面是一個簡單的消息轉(zhuǎn)換器定義,代碼如下:
@Configuration
@EnableWebMvc
public class WebConfiguration implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List> 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()));
}
}
復(fù)制代碼 在前面的示例中,Jackson2ObjectMapperBuilder 用于為 MappingJackson2HttpMessageConverter 和MappingJackson2XmlHttpMessageConverter 創(chuàng)建公共配置,啟用縮進(jìn)、自定義日期格式和jackson模塊參數(shù)名稱注冊,這增加了對訪問參數(shù)名稱的支持(Java8中添加的一個特性)。
配置 FastJson 為默認(rèn) JSON 解析器
其實我們在項目最最常用的還是 FastJSON 作為 Spring MVC 的默認(rèn) JSON 解析器。具體的配置如下:
class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
text/html;charset=UTF-8
application/json
application/xml;charset=UTF-8
復(fù)制代碼參考資料
docs.spring.io/spring-fram…
blog.csdn.net/qq_37292960…
作者:老鄭_
鏈接:https://juejin.cn/post/7019692901454069790
來源:稀土掘金
著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。
評論
圖片
表情

