spring boot 中的消息转换器(httpmessageconverter)是处理 http 请求和响应数据格式转换的核心组件,负责将 http 请求体中的数据转换为 java 对象,或将 java 对象转换为 http 响应体的数据。
核心接口
public interface httpmessageconverter<t> { boolean canread(class<?> clazz, @nullable mediatype mediatype); boolean canwrite(class<?> clazz, @nullable mediatype mediatype); list<mediatype> getsupportedmediatypes(); default list<mediatype> getsupportedmediatypes(class<?> clazz) { return !this.canread(clazz, (mediatype)null) && !this.canwrite(clazz, (mediatype)null) ? collections.emptylist() : this.getsupportedmediatypes(); } t read(class<? extends t> clazz, httpinputmessage inputmessage) throws ioexception, httpmessagenotreadableexception; void write(t t, @nullable mediatype contenttype, httpoutputmessage outputmessage) throws ioexception, httpmessagenotwritableexception; }
getsupportedmediatypes 定义支持的媒体类型(如:application/json)
canread
/canwrite
:判断是否支持读写操作
read
/write
:执行具体的数据转换逻辑
springboot默认提供的转换器
spring boot 自动配置了以下常用消息转换器:
- stringhttpmessageconverter - 处理文本数据
- mappingjackson2httpmessageconverter - 处理 json 数据(使用 jackson)
- formhttpmessageconverter - 处理表单数据
- bytearrayhttpmessageconverter - 处理字节数组
- resourcehttpmessageconverter - 处理资源文件
- jaxb2rootelementhttpmessageconverter - 处理 xml(使用 jaxb)
- allencompassingformhttpmessageconverter - 增强的表单处理器
springboot在启动的时候就会自动注册上述默认的转换器,有请求进来的时候会根据请求的accept和响应的content-type,选择匹配的转换器,若多个转换器都支持,则按注册顺序优先。
如何自定义消息转换器
比如自定义个fastjson转换器
import com.alibaba.fastjson2.json; import com.alibaba.fastjson2.support.config.fastjsonconfig; import org.springframework.http.httpinputmessage; import org.springframework.http.httpoutputmessage; import org.springframework.http.mediatype; import org.springframework.http.converter.abstracthttpmessageconverter; import org.springframework.http.converter.httpmessagenotreadableexception; import org.springframework.http.converter.httpmessagenotwritableexception; import org.springframework.lang.nonnull; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.lang.reflect.type; import java.nio.charset.standardcharsets; import java.util.arraylist; import java.util.list; public class fastjsonhttpmessageconverter extends abstracthttpmessageconverter<object> { private final fastjsonconfig fastjsonconfig = new fastjsonconfig(); public fastjsonhttpmessageconverter() { // 支持多种媒体类型 list<mediatype> supportedmediatypes = new arraylist<>(); supportedmediatypes.add(mediatype.application_json); supportedmediatypes.add(new mediatype("application", "*+json")); setsupportedmediatypes(supportedmediatypes); // 配置fastjson fastjsonconfig.setdateformat("yyyy-mm-dd hh:mm:ss"); fastjsonconfig.setcharset(standardcharsets.utf_8); } @override protected boolean supports(@nonnull class<?> clazz) { return true; // 支持所有类型 } @override protected object readinternal( @nonnull class<?> clazz, @nonnull httpinputmessage inputmessage ) throws ioexception, httpmessagenotreadableexception { try (inputstream in = inputmessage.getbody()) { return json.parseobject( in, fastjsonconfig.getcharset(), clazz, fastjsonconfig.getfeatures() ); } catch (exception e) { throw new httpmessagenotreadableexception( "json parse error: " + e.getmessage(), inputmessage ); } } @override protected void writeinternal( @nonnull object object, @nonnull httpoutputmessage outputmessage ) throws ioexception, httpmessagenotwritableexception { try (outputstream out = outputmessage.getbody()) { json.writeto( out, object, fastjsonconfig.getcharset(), fastjsonconfig.getfeatures(), fastjsonconfig.getfilters(), fastjsonconfig.getdateformat(), json.default_generate_feature, fastjsonconfig.getwriterfeatures() ); } catch (exception e) { throw new httpmessagenotwritableexception( "json write error: " + e.getmessage(), e ); } } public fastjsonconfig getfastjsonconfig() { return fastjsonconfig; } public void setfastjsonconfig(fastjsonconfig fastjsonconfig) { this.fastjsonconfig.setdateformat(fastjsonconfig.getdateformat()); this.fastjsonconfig.setcharset(fastjsonconfig.getcharset()); this.fastjsonconfig.setfeatures(fastjsonconfig.getfeatures()); this.fastjsonconfig.setreaderfeatures(fastjsonconfig.getreaderfeatures()); this.fastjsonconfig.setwriterfeatures(fastjsonconfig.getwriterfeatures()); this.fastjsonconfig.setfilters(fastjsonconfig.getfilters()); } }
注册自定义的消息转换器
import org.springframework.context.annotation.configuration; import org.springframework.http.converter.httpmessageconverter; import org.springframework.web.servlet.config.annotation.webmvcconfigurer; import java.util.list; @configuration public class fastjsonwebconfig implements webmvcconfigurer { @override public void configuremessageconverters(list<httpmessageconverter<?>> converters) { // 创建fastjson消息转换器 fastjsonhttpmessageconverter fastjsonconverter = new fastjsonhttpmessageconverter(); // 可以在这里进一步配置fastjson // fastjsonconverter.getfastjsonconfig().setdateformat("yyyy-mm-dd"); // 将fastjson转换器添加到转换器列表的最前面 converters.add(0, fastjsonconverter); } }
注意这里优先级的问题,要把fastjson的放前面,这样才会优先走咱自定义的fastjson的转换器,而不是默认的gson的
到此这篇关于聊聊springboot中如何自定义消息转换器的文章就介绍到这了,更多相关springboot 消息转换器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论