欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot響應處理實現(xiàn)流程詳解

 更新時間:2022年10月08日 11:25:37   作者:Decade0712  
這篇文章主要介紹了SpringBoot響應處理實現(xiàn)流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧

1、相關(guān)依賴

web項目引入的啟動器spring-boot-starter-web中含有

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-json</artifactId>
	<version>2.7.0</version>
	<scope>compile</scope>
</dependency>

這個依賴下面又有jackson的相關(guān)依賴,用于json的轉(zhuǎn)換

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.13.3</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jdk8</artifactId>
  <version>2.13.3</version>
  <scope>compile</scope>
</dependency>
<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-jsr310</artifactId>
  <version>2.13.3</version>
  <scope>compile</scope>
</dependency>

2、ReturnValueHandlers—返回值處理器

之前我們分析了參數(shù)解析器argumentResolvers的相關(guān)源碼,了解了請求中的參數(shù)是如何找到合適的參數(shù)解析器,并與方法的入?yún)⑦M行綁定的,那么問題來了,響應值的返回值處理器是如何找到的呢?

要分析源碼,我們還是得進入DispatcherServlet這個類下的doDiapatch()方法

與之前分析參數(shù)解析器一樣的流程,我們跟到了RequestMappingHandlerAdapter這個類下的invokeHandlerMethod()方法

通過斷點可以看到我們默認支持15種返回值解析器

那么他是怎么從這么多返回值解析器中選出自己支持的那一個呢?

我們可以發(fā)現(xiàn),這些返回值解析器都是實現(xiàn)了HandlerMethodReturnValueHandler接口

package org.springframework.web.method.support;
import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.web.context.request.NativeWebRequest;
public interface HandlerMethodReturnValueHandler {
    boolean supportsReturnType(MethodParameter returnType);
    void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception;
}

此接口下只有2個方法,一個是判斷是否支持此類型返回值,另一個是處理返回值的方法

是不是和之前的參數(shù)解析器的接口類有異曲同工之妙?

沒錯,我們正是使用這兩個方法來尋找適合自己的返回值解析器以及處理我們的返回值

跟著斷點一直向下,我們跟到了HandlerMethodReturnValueHandlerComposite類下的handleReturnValue()方法,在這里,我們就找到了對應的解析器并執(zhí)行了相關(guān)方法

public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
	// 獲取自己適合的返回值解析器
    HandlerMethodReturnValueHandler handler = this.selectHandler(returnValue, returnType);
    if (handler == null) {
        throw new IllegalArgumentException("Unknown return value type: " + returnType.getParameterType().getName());
    } else {
    	// 調(diào)用該返回值解析器中的處理返回值的方法
        handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
    }
}
@Nullable
private HandlerMethodReturnValueHandler selectHandler(@Nullable Object value, MethodParameter returnType) {
	// 判斷是否是異步的返回值
    boolean isAsyncValue = this.isAsyncReturnValue(value, returnType);
    Iterator var4 = this.returnValueHandlers.iterator();
    HandlerMethodReturnValueHandler handler;
    // 使用do-while進行遍歷,找出支持當前返回值類型的解析器
    do {
        do {
            if (!var4.hasNext()) {
                return null;
            }

            handler = (HandlerMethodReturnValueHandler)var4.next();
        } while(isAsyncValue && !(handler instanceof AsyncHandlerMethodReturnValueHandler));
    } while(!handler.supportsReturnType(returnType));
    return handler;
}

最終,我們確定使用解析器RequestResponseBodyMethodProcessor可以處理標注了@ResponseBody的返回值

3、HttpMessageConvert—消息轉(zhuǎn)換器

除了尋找合適的返回值解析器之外,我們還有一個問題要思考

為什么我們在控制器類的方法上加一個@ResponseBody注解就能將響應信息轉(zhuǎn)換成json格式呢?

這個轉(zhuǎn)換是怎樣實現(xiàn)的呢?

我們接著上面的代碼繼續(xù)往下debug

之前我們已經(jīng)定位到了使用RequestResponseBodyMethodProcessor來處理@ResponseBody的返回值,所以我們繼續(xù)深入到這個類下的handleReturnValue()

public void handleReturnValue(@Nullable Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
	mavContainer.setRequestHandled(true);
	// 創(chuàng)建輸入和輸出信息
	ServletServerHttpRequest inputMessage = this.createInputMessage(webRequest);
	ServletServerHttpResponse outputMessage = this.createOutputMessage(webRequest);
	// 使用消息轉(zhuǎn)換器進行寫出操作
	this.writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);
}

然后我們來分析解析器父類AbstractMessageConverterMethodProcessor下的writeWithMessageConverters()方法

首先我們了解一個概念:什么是內(nèi)容協(xié)商?

瀏覽器默認會以請求頭的方式告訴服務器他能接受什么樣的內(nèi)容類型,這里的q代表優(yōu)先級

然后服務器最終根據(jù)自己自身的能力,決定服務器能生產(chǎn)出什么樣內(nèi)容類型的數(shù)據(jù)

所以在writeWithMessageConverters()方法中我們獲取到瀏覽器能接受什么內(nèi)容

以及服務器能產(chǎn)生什么內(nèi)容

然后我們經(jīng)過協(xié)商,決定返回application/json格式的數(shù)據(jù)

重點來了:這里有一系列的消息轉(zhuǎn)換器,我們到底使用哪個呢?

隨便點進一個消息轉(zhuǎn)換器,我們發(fā)現(xiàn)他都是實現(xiàn)了HttpMessageConverter這個接口

這里面有2個方法,canRead()和canWrite()來幫助我們判斷能否支持源類型和目標類型的消息讀寫

最終我們確定了,使用MappingJackson2HttpMessageConverter這個消息轉(zhuǎn)換器來進行json轉(zhuǎn)換

package org.springframework.http.converter;
import java.io.IOException;
import java.util.Collections;
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> {
	// 讀取,將我們當前消息轉(zhuǎn)換器支持的對象以某種格式讀取進來,例如將json數(shù)據(jù)讀取成Person對象
    boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);
	// 寫出,我們當前消息轉(zhuǎn)換器支持的對象以某種格式寫出去,例如將Person對象以json格式寫出去
    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;
}

然后我們在此處調(diào)用MappingJackson2HttpMessageConverter父類AbstractGenericHttpMessageConverter中的write()方法

走到AbstractJackson2HttpMessageConverter類下的writeInternal()方法

可以看出,它利用了底層的Jackson的objectMapper進行轉(zhuǎn)換

這樣,我們就完成了Person數(shù)據(jù)到json格式的轉(zhuǎn)換

大概流程如下

  • 判斷當前響應頭中是否已經(jīng)有確定的媒體類型,即MediaType
  • 獲取客戶端(PostMan、瀏覽器)支持接收的內(nèi)容類型(獲取客戶端Accept請求頭字段)
  • 遍歷循環(huán)所有當前系統(tǒng)的 MessageConverter,看誰支持操作這個對象(Person),找到支持操作Person的converter,把該converter支持的媒體類型統(tǒng)計出來放到集合中,這樣就獲取到服務端能提供哪些媒體類型
  • 進行內(nèi)容協(xié)商,找到最佳匹配媒體類型
  • 用 支持 將對象轉(zhuǎn)為 最佳匹配媒體類型 的converter,調(diào)用它進行轉(zhuǎn)化

4、開啟瀏覽器參數(shù)方式內(nèi)容協(xié)商功能

如果需要返回xml格式的數(shù)據(jù),那么需要額外導入相關(guān)依賴

<dependency>
	<groupId>com.fasterxml.jackson.dataformat</groupId>
	<artifactId>jackson-dataformat-xml</artifactId>
</dependency>

我們可以看到,瀏覽器是默認支持如下返回格式的,一般情況下,我們無法指定自己需要的返回格式

但是我們可以通過修改配置+添加參數(shù)的方式指定我們需要的格式

首先,我們在yaml文件中,開啟基于瀏覽器參數(shù)方式內(nèi)容協(xié)商功能

spring:
  mvc:
    contentnegotiation:
      favor-parameter: true

一旦此參數(shù)設置為true,那么我們的內(nèi)容協(xié)商管理器contentNegotiationManager中,除了原有的從請求頭獲取媒體類型的策略之外,還多了一個從請求參數(shù)中獲取媒體類型的策略,它支持xml和json兩種媒體類型

然后我們使用http://localhost:8080/testPathVariable/001/split/decade?format=xml或者http://localhost:8080/testPathVariable/001/split/decade?format=json指定我們需要的返回格式

到此這篇關(guān)于SpringBoot響應處理實現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)SpringBoot響應處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論