站点工具


服务器技术:java:springmvc之httpmessageconverter类

差别

这里会显示出您选择的修订版和当前版本之间的差别。

到此差别页面的链接

服务器技术:java:springmvc之httpmessageconverter类 [2020/03/03 15:50]
admin 创建
服务器技术:java:springmvc之httpmessageconverter类 [2020/03/03 15:50] (当前版本)
admin 创建
行 1: 行 1:
 +<​p>​HttpMessageConverter是Spring的一个重要接口,它负责将请求信息转换为一个对象,将对象输出为响应信息。<​br/><​br/>​DispatcherServlet默认已经安装了RequestMappingHandlerAdapter作为HandlerAdapter的组件实现类,HttpMessageConverter即由RequestMappingHandlerAdapter使用,将请求信息转换为对象,或将对象转换为响应信息。<​br/><​br/>​HttpMessageConverter接口定义了以下几个方法。<​br/><​br/>​① Boolean canRead(Class&​lt;?&​gt;​ clazz,​MediaType mediaType):<​br/><​br/>​指定转换器可以读取的媒体类型(如text/​html、application/​json等),MIME媒体类型在RFC2616中定义,详细可以参考:http://​www.w3school.com.cn/​media/​media_mimeref.asp 。<​br/><​br/>​② Boolean canWrite(Class&​lt;?&​gt;​ clazz,​MediaType mediaType):<​br/><​br/>​指定转换器可以将clazz类型的对象写道响应流中,响应流支持的媒体类型在mediaType中定义。<​br/><​br/>​③ List&​lt;​MediaType&​gt;​ getSupportedMediaTypes():<​br/><​br/>​该转换器支持的媒体类型。<​br/><​br/>​④ T read(Class&​lt;?​ extends T&gt; clazz,​HttpInputMessage inputMessage):<​br/><​br/>​将请求信息流转换为T类型的对象。<​br/><​br/>​⑤ void write(T t,MediaType contentType,​HttpOutputMessage outputMessage):<​br/><​br/>​将T类型的对象写到响应流中,同时指定响应的媒体类型为contentType。</​p><​p>​————————————————<​br/>​版权声明:本文为CSDN博主「yongqi_wang」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。<​br/>​原文链接:</​p><​div><​a href="​https://​blog.csdn.net/​yongqi_wang/​article/​details/​87165241">​https://​blog.csdn.net/​yongqi_wang/​article/​details/​87165241</​a></​div><​p><​br/></​p><​p><​br/></​p><​p>​import java.io.IOException;<​br/>​import java.lang.reflect.Type;<​br/>​import java.nio.charset.Charset;<​br/>​import java.util.HashMap;<​br/>​import java.util.List;<​br/><​br/>​import org.apache.poi.ss.formula.functions.T;<​br/>​import org.springframework.http.HttpInputMessage;<​br/>​import org.springframework.http.HttpOutputMessage;<​br/>​import org.springframework.http.MediaType;<​br/>​import org.springframework.http.converter.AbstractHttpMessageConverter;<​br/>​import org.springframework.http.converter.GenericHttpMessageConverter;<​br/>​import org.springframework.http.converter.HttpMessageConverter;<​br/>​import org.springframework.http.converter.HttpMessageNotReadableException;<​br/>​import org.springframework.http.converter.HttpMessageNotWritableException;<​br/>​import org.springframework.util.StreamUtils;<​br/><​br/>​import com.alibaba.fastjson.JSON;<​br/>​import com.amway.pos.base.model.comm.WebRespBody;<​br/><​br/><​br/>​public class RestMessageConverter extends AbstractHttpMessageConverter&​lt;​Object&​gt;​ implements GenericHttpMessageConverter&​lt;​Object&​gt;​ {<​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;​private boolean needGzip = false;<​br/>&​nbsp;&​nbsp;​ &​nbsp;//​ 自定义媒体类型<​br/>&​nbsp;&​nbsp;​ &​nbsp;​public RestMessageConverter() {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​super(new MediaType(&​quot;​application&​quot;,​ &​quot;​x-wisely&​quot;,​ Charset.forName(&​quot;​UTF-8&​quot;​)),&​nbsp;​ new MediaType(&​quot;​application&​quot;,​ &​quot;​json&​quot;,​ Charset.forName(&​quot;​UTF-8&​quot;​)));<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/>&​nbsp;&​nbsp;​ &​nbsp;<​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;<​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;//​ 只支持WebRespBody类<​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Override<​br/>&​nbsp;&​nbsp;​ &​nbsp;​protected boolean supports(Class&​lt;?&​gt;​ clazz) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​return T.class.isAssignableFrom(clazz);<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;//​ 配置WiselyMessageConverter<​br/>&​nbsp;&​nbsp;​ &​nbsp;​public void extendMessageConverters(List&​lt;​HttpMessageConverter&​lt;?&​gt;&​gt;​ converters) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​RestMessageConverter converter = new RestMessageConverter();<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​converters.add(converter);<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;<​br/><​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;//​ 从request里获得构造WebRespBody实例的数据<​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Override<​br/>&​nbsp;&​nbsp;​ &​nbsp;​protected Object readInternal(Class&​lt;?&​gt;​ clazz, HttpInputMessage inputMessage)<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​throws IOException,​ HttpMessageNotReadableException {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;//​ TODO Auto-generated method stub<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​String temp = StreamUtils.copyToString(inputMessage.getBody(),​ Charset.forName(&​quot;​UTF-8&​quot;​));<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​System.out.print(&​quot;​\n==========================&​quot;​+temp+&​quot;​\n=========================================&​quot;​);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​return null;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;//​ 将WebRespBody实例转换成你想要的字符串格式<​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Override<​br/>&​nbsp;&​nbsp;​ &​nbsp;​protected void writeInternal(Object t, HttpOutputMessage outputMessage)<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​throws IOException,​ HttpMessageNotWritableException {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​String out = &​quot;​hello:​\n====================================&​quot;​ + t.toString();<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​outputMessage.getBody().write(out.getBytes());<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/><​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Override<​br/>&​nbsp;&​nbsp;​ &​nbsp;​public boolean canRead(Type type, Class&​lt;?&​gt;​ contextClass,​ MediaType mediaType) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;//​ TODO Auto-generated method stub<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​if(&​quot;​json&​quot;​.equals(mediaType.getSubtype())){<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​return true;<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​}<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​return false;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/><​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Override<​br/>&​nbsp;&​nbsp;​ &​nbsp;​public Object read(Type type, Class&​lt;?&​gt;​ contextClass,​ HttpInputMessage inputMessage)<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​throws IOException,​ HttpMessageNotReadableException {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;//​ TODO Auto-generated method stub<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​System.out.println(&​quot;​Object read(Type type, Class&​lt;?&​gt;​ contextClass,​ HttpInputMessage inputMessage)&​quot;​);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;//​String temp = StreamUtils.copyToString(inputMessage.getBody(),​ Charset.forName(&​quot;​UTF-8&​quot;​));<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;//​System.out.print(&​quot;​\n==========================&​quot;​+temp+&​quot;​\n=========================================&​quot;​);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​Object obj = JSON.parseObject(inputMessage.getBody(),​ type);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​return obj;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/><​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Override<​br/>&​nbsp;&​nbsp;​ &​nbsp;​public boolean canWrite(Type type, Class&​lt;?&​gt;​ clazz, MediaType mediaType) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;//​ TODO Auto-generated method stub<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​return true;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/><​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Override<​br/>&​nbsp;&​nbsp;​ &​nbsp;​public void write(Object t, Type type, MediaType contentType,​ HttpOutputMessage outputMessage)<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​throws IOException,​ HttpMessageNotWritableException {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​Object data = null;<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​if (t instanceof HashMap) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; data = ((HashMap) t).get(&​quot;​data&​quot;​);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; if (needGzip) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; String str = JSON.toJSONString(data);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; data = GZIPUtils.toString(GZIPUtils.compress(str));<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; ((HashMap) t).put(&​quot;​data&​quot;,​ data);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​}<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​}<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​if (t instanceof WebRespBody) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; data = ((WebRespBody) t).getData();<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; if (needGzip) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; String str = JSON.toJSONString(data);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; data = GZIPUtils.toString(GZIPUtils.compress(str));<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &nbsp; ((WebRespBody) t).setData(data);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​}<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​}<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​String json = JSON.toJSONString(t);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​outputMessage.getHeaders().add(&​quot;​Content-Type&​quot;,​ &​quot;​application/​json;​charset=UTF-8&​quot;​);<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​outputMessage.getBody().write(json.getBytes(&​quot;​UTF-8&​quot;​));<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/><​br/><​br/><​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;​public boolean isNeedGzip() {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​return needGzip;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/><​br/><​br/><​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;​public void setNeedGzip(boolean needGzip) {<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​this.needGzip = needGzip;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/><​br/><​br/><​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;<​br/>​}</​p><​p><​br/></​p><​p><​br/></​p><​p>​@Configuration<​br/>​public class RestMessageConverterConfig {<​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Value(&​quot;​${restapi.response.needgzip:​false}&​quot;​)<​br/>&​nbsp;&​nbsp;​ &​nbsp;​private boolean needgzip = false;<​br/>&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;//​ 引入Fastjson解析json,不使用默认的jackson<​br/>&​nbsp;&​nbsp;​ &​nbsp;//​ 必须在pom.xml引入fastjson的jar包,并且版必须大于1.2.10<​br/>&​nbsp;&​nbsp;​ &​nbsp;​@Bean<​br/>&​nbsp;&​nbsp;​ &​nbsp;​public HttpMessageConverters fastJsonHttpMessageConverters() {<​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​RestMessageConverter wiselyMessageConverter = new RestMessageConverter();<​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​wiselyMessageConverter.setNeedGzip(needgzip);<​br/><​br/>&​nbsp;&​nbsp;​ &​nbsp;&​nbsp;&​nbsp;​ &​nbsp;​return new HttpMessageConverters(wiselyMessageConverter,​ converter );<​br/>&​nbsp;&​nbsp;​ &​nbsp;​}<​br/></​p><​p><​br/>​ </p>
服务器技术/java/springmvc之httpmessageconverter类.txt · 最后更改: 2020/03/03 15:50 由 admin