一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - JAVA教程 - Spring HttpMessageConverter的作用及替換解析

Spring HttpMessageConverter的作用及替換解析

2021-04-01 14:49weknow619 JAVA教程

這篇文章主要介紹了Spring HttpMessageConverter的作用及替換解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

相信使用過spring的開發(fā)人員都用過@requestbody、@responsebody注解,可以直接將輸入解析成json、將輸出解析成json,但http 請求和響應是基于文本的,意味著瀏覽器和服務器通過交換原始文本進行通信,而這里其實就是httpmessageconverter發(fā)揮著作用。

httpmessageconverter

http請求響應報文其實都是字符串,當請求報文到java程序會被封裝為一個servletinputstream流,開發(fā)人員再讀取報文,響應報文則通過servletoutputstream流,來輸出響應報文。

從流中只能讀取到原始的字符串報文,同樣輸出流也是。那么在報文到達springmvc / springboot和從springmvc / springboot出去,都存在一個字符串到java對象的轉(zhuǎn)化問題。這一過程,在springmvc / springboot中,是通過httpmessageconverter來解決的。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;
}

下面以一例子來說明,

?
1
2
3
4
5
@requestmapping("/test")
@responsebody
public string test(@requestbody string param) {
  return "param '" + param + "'";
}

在請求進入test方法前,會根據(jù)@requestbody注解選擇對應的httpmessageconverter實現(xiàn)類來將請求參數(shù)解析到param變量中,因為這里的參數(shù)是string類型的,所以這里是使用了stringhttpmessageconverter類,它的canread()方法返回true,然后read()方法會從請求中讀出請求參數(shù),綁定到test()方法的param變量中。

同理當執(zhí)行test方法后,由于返回值標識了@responsebody,springmvc / springboot將使用stringhttpmessageconverter的write()方法,將結(jié)果作為string值寫入響應報文,當然,此時canwrite()方法返回true。

借用下圖簡單描述整個過程:

Spring HttpMessageConverter的作用及替換解析

在spring的處理過程中,一次請求報文和一次響應報文,分別被抽象為一個請求消息httpinputmessage和一個響應消息httpoutputmessage。

處理請求時,由合適的消息轉(zhuǎn)換器將請求報文綁定為方法中的形參對象,在這里同一個對象就有可能出現(xiàn)多種不同的消息形式,如json、xml。同樣響應請求也是同樣道理。

在spring中,針對不同的消息形式,有不同的httpmessageconverter實現(xiàn)類來處理各種消息形式,至于各種消息解析實現(xiàn)的不同,則在不同的httpmessageconverter實現(xiàn)類中。

替換@responsebody默認的httpmessageconverter

這里使用springboot演示例子,在springmvc / springboot中@requestbody這類注解默認使用的是jackson來解析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;
  }
}

瀏覽器訪問/user/testt返回如下:

Spring HttpMessageConverter的作用及替換解析

這就是使用jackson解析的結(jié)果,現(xiàn)在來改成使用fastjson解析對象,這里就是替換默認的httpmessageconverter,就是將其改成使用fastjsonhttpmessageconverter來處理java對象與httpinputmessage/httpoutputmessage間的轉(zhuǎn)化。

首先新建一配置類來添加配置fastjsonhttpmessageconverter,spring4.x開始推薦使用java配置加注解的方式,也就是無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,不使用默認的jackson
  //必須在pom.xml引入fastjson的jar包,并且版必須大于1.2.10
  @bean
  public httpmessageconverters fastjsonhttpmessageconverters() {
    //1、定義一個convert轉(zhuǎn)換消息的對象
    fastjsonhttpmessageconverter fastconverter = new fastjsonhttpmessageconverter();
 
    //2、添加fastjson的配置信息
    fastjsonconfig fastjsonconfig = new fastjsonconfig();
 
    serializerfeature[] serializerfeatures = new serializerfeature[]{
        //  輸出key是包含雙引號
//        serializerfeature.quotefieldnames,
        //  是否輸出為null的字段,若為null 則顯示該字段
//        serializerfeature.writemapnullvalue,
        //  數(shù)值字段如果為null,則輸出為0
        serializerfeature.writenullnumberaszero,
        //   list字段如果為null,輸出為[],而非null
        serializerfeature.writenulllistasempty,
        //  字符類型字段如果為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);
  }
}

這里將字符串類型的值如果是null就返回“”,數(shù)值類型的如果是null就返回0,重啟應用,再次訪問/user/testt接口,返回如下:

Spring HttpMessageConverter的作用及替換解析

可以看到此時null都轉(zhuǎn)化成“”或0了。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/weknow619/p/8422382.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美性4khd720 | 四虎精品永久免费 | 国产在线观看91精品一区 | 四虎最新永久免费网址 | 国产精品久久久久久久牛牛 | 丰满大屁股美女一级毛片 | 男女全黄h全肉细节文 | 精品亚洲午夜久久久久 | 麻豆在线md0087免费 | jizzjizz3d动漫| julianann办公室 | 国产在线观看精品 | 2018高清国产一道国产 | 精品欧美男同同性videos | 亚洲乱码一二三四区国产 | 天天射寡妇射 | 欧美成人v视频免费看 | 国产东北3p真实在线456视频 | 福利色播 | 91在线永久 | 亚洲国产精品自在自线观看 | 久久伊人电影 | 国产欧美曰韩一区二区三区 | 女主被男主为催奶药h | 国产亚洲一区二区三区 | www.亚洲5555.com | 久久综合狠狠综合久久综合88 | 99精品国产成人a∨免费看 | 国产视频一区二 | 国产成年人网站 | 免费精品视频在线 | 亚洲国产精品无码中文在线 | 国产中文在线视频 | 四虎国产免费 | 国产日韩精品欧美一区 | 动漫女性扒开尿口羞羞漫画 | 亚洲国产在线观看免费视频 | 大桥未久一区二区 | 俄罗斯一级淫片bbbb | 激情艳妇| 农村妇女野外性生话免费视频 |