相信使用過(guò)spring的開(kāi)發(fā)人員都用過(guò)@requestbody、@responsebody注解,可以直接將輸入解析成json、將輸出解析成json,但http 請(qǐng)求和響應(yīng)是基于文本的,意味著瀏覽器和服務(wù)器通過(guò)交換原始文本進(jìn)行通信,而這里其實(shí)就是httpmessageconverter發(fā)揮著作用。
httpmessageconverter
http請(qǐng)求響應(yīng)報(bào)文其實(shí)都是字符串,當(dāng)請(qǐng)求報(bào)文到j(luò)ava程序會(huì)被封裝為一個(gè)servletinputstream流,開(kāi)發(fā)人員再讀取報(bào)文,響應(yīng)報(bào)文則通過(guò)servletoutputstream流,來(lái)輸出響應(yīng)報(bào)文。
從流中只能讀取到原始的字符串報(bào)文,同樣輸出流也是。那么在報(bào)文到達(dá)springmvc / springboot和從springmvc / springboot出去,都存在一個(gè)字符串到j(luò)ava對(duì)象的轉(zhuǎn)化問(wèn)題。這一過(guò)程,在springmvc / springboot中,是通過(guò)httpmessageconverter來(lái)解決的。httpmessageconverter接口源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public interface httpmessageconverter<t> { boolean canread( class <?> clazz, mediatype mediatype); boolean canwrite( class <?> clazz, mediatype mediatype); list<mediatype> getsupportedmediatypes(); t read( class <? extends t> clazz, httpinputmessage inputmessage) throws ioexception, httpmessagenotreadableexception; void write(t t, mediatype contenttype, httpoutputmessage outputmessage) throws ioexception, httpmessagenotwritableexception; } |
下面以一例子來(lái)說(shuō)明,
1
2
3
4
5
|
@requestmapping ( "/test" ) @responsebody public string test( @requestbody string param) { return "param '" + param + "'" ; } |
在請(qǐng)求進(jìn)入test方法前,會(huì)根據(jù)@requestbody注解選擇對(duì)應(yīng)的httpmessageconverter實(shí)現(xiàn)類(lèi)來(lái)將請(qǐng)求參數(shù)解析到param變量中,因?yàn)檫@里的參數(shù)是string類(lèi)型的,所以這里是使用了stringhttpmessageconverter類(lèi),它的canread()方法返回true,然后read()方法會(huì)從請(qǐng)求中讀出請(qǐng)求參數(shù),綁定到test()方法的param變量中。
同理當(dāng)執(zhí)行test方法后,由于返回值標(biāo)識(shí)了@responsebody,springmvc / springboot將使用stringhttpmessageconverter的write()方法,將結(jié)果作為string值寫(xiě)入響應(yīng)報(bào)文,當(dāng)然,此時(shí)canwrite()方法返回true。
借用下圖簡(jiǎn)單描述整個(gè)過(guò)程:
在spring的處理過(guò)程中,一次請(qǐng)求報(bào)文和一次響應(yīng)報(bào)文,分別被抽象為一個(gè)請(qǐng)求消息httpinputmessage和一個(gè)響應(yīng)消息httpoutputmessage。
處理請(qǐng)求時(shí),由合適的消息轉(zhuǎn)換器將請(qǐng)求報(bào)文綁定為方法中的形參對(duì)象,在這里同一個(gè)對(duì)象就有可能出現(xiàn)多種不同的消息形式,如json、xml。同樣響應(yīng)請(qǐng)求也是同樣道理。
在spring中,針對(duì)不同的消息形式,有不同的httpmessageconverter實(shí)現(xiàn)類(lèi)來(lái)處理各種消息形式,至于各種消息解析實(shí)現(xiàn)的不同,則在不同的httpmessageconverter實(shí)現(xiàn)類(lèi)中。
替換@responsebody默認(rèn)的httpmessageconverter
這里使用springboot演示例子,在springmvc / springboot中@requestbody這類(lèi)注解默認(rèn)使用的是jackson來(lái)解析json,看下面例子:
1
2
3
4
5
6
7
8
9
10
11
|
@controller @requestmapping ( "/user" ) public class usercontroller { @requestmapping ( "/testt" ) @responsebody public user testt() { user user = new user( "name" , 18 ); return user; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public class user { private string username; private integer age; private integer phone; private string email; public user(string username, integer age) { super (); this .username = username; this .age = age; } } |
瀏覽器訪問(wèn)/user/testt返回如下:
這就是使用jackson解析的結(jié)果,現(xiàn)在來(lái)改成使用fastjson解析對(duì)象,這里就是替換默認(rèn)的httpmessageconverter,就是將其改成使用fastjsonhttpmessageconverter來(lái)處理java對(duì)象與httpinputmessage/httpoutputmessage間的轉(zhuǎn)化。
首先新建一配置類(lèi)來(lái)添加配置fastjsonhttpmessageconverter,spring4.x開(kāi)始推薦使用java配置加注解的方式,也就是無(wú)xml文件,springboot就更是了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
import com.alibaba.fastjson.serializer.serializerfeature; import com.alibaba.fastjson.support.config.fastjsonconfig; import com.alibaba.fastjson.support.spring.fastjsonhttpmessageconverter; import org.springframework.boot.autoconfigure.web.httpmessageconverters; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.http.converter.httpmessageconverter; import java.nio.charset.charset; @configuration public class httpmessageconverterconfig { //引入fastjson解析json,不使用默認(rèn)的jackson //必須在pom.xml引入fastjson的jar包,并且版必須大于1.2.10 @bean public httpmessageconverters fastjsonhttpmessageconverters() { //1、定義一個(gè)convert轉(zhuǎn)換消息的對(duì)象 fastjsonhttpmessageconverter fastconverter = new fastjsonhttpmessageconverter(); //2、添加fastjson的配置信息 fastjsonconfig fastjsonconfig = new fastjsonconfig(); serializerfeature[] serializerfeatures = new serializerfeature[]{ // 輸出key是包含雙引號(hào) // serializerfeature.quotefieldnames, // 是否輸出為null的字段,若為null 則顯示該字段 // serializerfeature.writemapnullvalue, // 數(shù)值字段如果為null,則輸出為0 serializerfeature.writenullnumberaszero, // list字段如果為null,輸出為[],而非null serializerfeature.writenulllistasempty, // 字符類(lèi)型字段如果為null,輸出為"",而非null serializerfeature.writenullstringasempty, // boolean字段如果為null,輸出為false,而非null serializerfeature.writenullbooleanasfalse, // date的日期轉(zhuǎn)換器 serializerfeature.writedateusedateformat, // 循環(huán)引用 serializerfeature.disablecircularreferencedetect, }; fastjsonconfig.setserializerfeatures(serializerfeatures); fastjsonconfig.setcharset(charset.forname( "utf-8" )); //3、在convert中添加配置信息 fastconverter.setfastjsonconfig(fastjsonconfig); //4、將convert添加到converters中 httpmessageconverter<?> converter = fastconverter; return new httpmessageconverters(converter); } } |
這里將字符串類(lèi)型的值如果是null就返回“”,數(shù)值類(lèi)型的如果是null就返回0,重啟應(yīng)用,再次訪問(wèn)/user/testt接口,返回如下:
可以看到此時(shí)null都轉(zhuǎn)化成“”或0了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/weknow619/p/8422382.html