使用HttpMessageConverter實現HTTP的序列化和反序列化


對象的序列化/反序列化大家應該都比較熟悉:序列化就是將object轉化為可以傳輸的二進制,反序列化就是將二進制轉化為程序內部的對象。序列化/反序列化主要體現在程序I/O這個過程中,包括網絡I/O和磁盤I/O。

那么什么是http序列化和反序列化呢?

在使用springmvc/SpringBoot時,我們經常會這樣寫:

@RequestMapping("/test")
@ResponseBody public String test(@RequestBody String param) {
    return "param '" + param + "'";
}

 

@RestController中有@ResponseBody,可以幫我們把User序列化到resp.body中。@RequestBody可以幫我們把req.body的內容轉化為User對象。如果是開發Web應用,一般這兩個注解對應的就是Json序列化和反序列化的操作。這里實際上已經體現了Http序列化/反序列化這個過程,只不過和普通的對象序列化有些不一樣,Http序列化/反序列化的層次更高,屬於一種Object2Object之間的轉換。

有過Netty使用經驗的對這個應該比較了解,Netty中的Decoder和Encoder就有兩種基本層次,層次低的一種是Byte <—> Message,二進制與程序內部消息對象之間的轉換,就是常見的序列化/反序列化;另外一種是 Message <—> Message,程序內部對象之間的轉換,比較高層次的序列化/反序列化。

Http協議的處理過程,TCP字節流 <—> HttpRequest/HttpResponse <—> 內部對象,就涉及這兩種序列化。在springmvc中第一步已經由Servlet容器(tomcat等等)幫我們處理了,第二步則主要由框架幫我們處理。上面所說的Http序列化/反序列化就是指的這第二個步驟,它是controller層框架的核心功能之一,有了這個功能,就能大大減少代碼量,讓controller的邏輯更簡潔清晰,就像上面示意的代碼那樣,方法中只有一行代碼。spirngmvc進行第二步操作,也就是Http序列化和反序列化的核心是HttpMessageConverter。

HttpMessageConverter

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

從流中只能讀取到原始的字符串報文,同樣輸出流也是。那么在報文到達SpringMVC / SpringBoot和從SpringMVC / SpringBoot出去,都存在一個字符串到java對象的轉化問題。這一過程,在SpringMVC / SpringBoot中,是通過HttpMessageConverter來解決的。HttpMessageConverter接口源碼:

package org.springframework.http.converter;

import java.io.IOException;
import java.util.List;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;

public interface HttpMessageConverter<T> {
    boolean canRead(Class<?> var1, @Nullable MediaType var2);

    boolean canWrite(Class<?> var1, @Nullable MediaType var2);

    List<MediaType> getSupportedMediaTypes();

    T read(Class<? extends T> var1, HttpInputMessage var2) throws IOException, HttpMessageNotReadableException;

    void write(T var1, @Nullable MediaType var2, HttpOutputMessage var3) throws IOException, HttpMessageNotWritableException;
}

 

以上面的代碼為例子來說明一下:在請求進入test方法前,會根據@RequestBody注解選擇對應的HttpMessageConverter實現類來將請求參數解析到param變量中,因為這里的參數是String類型的,所以這里是使用了StringHttpMessageConverter類,它的canRead()方法返回true,然后read()方法會從請求中讀出請求參數,綁定到test()方法的param變量中。

同理當執行test方法后,由於返回值標識了@ResponseBody,SpringMVC / SpringBoot將使用StringHttpMessageConverter的write()方法,將結果作為String值寫入響應報文,當然,此時canWrite()方法返回true。

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

在Spring的處理過程中,一次請求報文和一次響應報文,分別被抽象為一個請求消息HttpInputMessage和一個響應消息HttpOutputMessage。處理請求時,由合適的消息轉換器將請求報文綁定為方法中的形參對象,在這里同一個對象就有可能出現多種不同的消息形式,如json、xml。同樣響應請求也是同樣道理。在Spring中,針對不同的消息形式,有不同的HttpMessageConverter實現類來處理各種消息形式,至於各種消息解析實現的不同,則在不同的HttpMessageConverter實現類中。

替換@ResponseBody默認的HttpMessageConverter

1、springboot框架默認的使用jackson進行json轉化

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;
    }
}

 

 

@Controller
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/testt")
    @ResponseBody
    public User testt() {
        User user = new User("name", 18);
        return user;
    }
}

 

瀏覽器訪問/user/testt請求結果如下:

這就是使用Jackson解析的結果,沒有傳值的字段默認被解析成了null。現在來改成使用fastjson解析對象,這里就是替換默認的HttpMessageConverter,就是將其改成使用FastJsonHttpMessageConverter來處理Java對象與HttpInputMessage/HttpOutputMessage間的轉化。

2、使用fastjson替代默認的jackson轉化方式

增加Fastjson的maven依賴

<dependency>
     <groupId>com.alibaba</groupId>
     <artifactId>fastjson</artifactId>
     <version>1.2.47</version>
</dependency>

Springboot配置FastJsonHttpMessageConverter有兩種方法:

方法一:啟動類繼承extends WebMvcConfigurerAdapter,然后覆蓋方法configureMessageConverters

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

/** springboot以fastjon方式轉化json數據 */
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter {
    private static Logger logger = Logger.getLogger(Application.class);

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        super.configureMessageConverters(converters);
        //1.需要定義一個convert轉換消息的對象;
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        //2.添加fastJson的配置信息,比如:是否要格式化返回的json數據;
        FastJsonConfig fastJsonConfig = new FastJsonConfig();

    SerializerFeature[] serializerFeatures = new SerializerFeature[]{
                //    輸出key是包含雙引號
//                SerializerFeature.QuoteFieldNames,
                //    是否輸出為null的字段,若為null 則顯示該字段
//                SerializerFeature.WriteMapNullValue,
                //    數值字段如果為null,則輸出為0
                SerializerFeature.WriteNullNumberAsZero,
                //     List字段如果為null,輸出為[],而非null
                SerializerFeature.WriteNullListAsEmpty,
                //    字符類型字段如果為null,輸出為"",而非null
                SerializerFeature.WriteNullStringAsEmpty,
                //    Boolean字段如果為null,輸出為false,而非null
                SerializerFeature.WriteNullBooleanAsFalse,
                //    Date的日期轉換器
                SerializerFeature.WriteDateUseDateFormat,
                //    循環引用
                SerializerFeature.DisableCircularReferenceDetect,
        };

        fastJsonConfig.setSerializerFeatures(serializerFeatures);
        //3處理中文亂碼問題
        List<MediaType> fastMediaTypes = new ArrayList<>();
        fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        //4.在convert中添加配置信息.
        fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        //5.將convert添加到converters當中.
        converters.add(fastJsonHttpMessageConverter);
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
        logger.info("=====spring boot start success====");
    }
}

 

 

方法二:添加配置類來注入Bean:HttpMessageConverters

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轉換消息的對象
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //2、添加fastjson的配置信息
        FastJsonConfig fastJsonConfig = new FastJsonConfig();

        SerializerFeature[] serializerFeatures = new SerializerFeature[]{
                //    輸出key是包含雙引號
//                SerializerFeature.QuoteFieldNames,
                //    是否輸出為null的字段,若為null 則顯示該字段
//                SerializerFeature.WriteMapNullValue,
                //    數值字段如果為null,則輸出為0
                SerializerFeature.WriteNullNumberAsZero,
                //     List字段如果為null,輸出為[],而非null
                SerializerFeature.WriteNullListAsEmpty,
                //    字符類型字段如果為null,輸出為"",而非null
                SerializerFeature.WriteNullStringAsEmpty,
                //    Boolean字段如果為null,輸出為false,而非null
                SerializerFeature.WriteNullBooleanAsFalse,
                //    Date的日期轉換器
                SerializerFeature.WriteDateUseDateFormat,
                //    循環引用
                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);
    }
}

 

瀏覽器發起請求,得到的結果如下:

 

 

未傳值的字符串類型的屬性被解析為””,未傳值的數值類型的屬性被解析為0。

參考資料:
1、【Spring】HttpMessageConverter的作用及替換
2、springboot學習(三)——使用HttpMessageConverter進行http序列化和反序列化
3、SpringBoot-擴展FastJsonHttpMessageConverter對返回的json對象進行擴展
4、fastjson SerializerFeature詳解

原文鏈接:https://blog.csdn.net/lmb55/article/details/90676823


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM