SpringMVC MethodArgumentResolver的作用與實現(xiàn)
昨天的文章我們學(xué)習(xí) MethodArgumentResolver ,它是采用一種策略模式,在 Handler 的方法被調(diào)用前,將 HTTP 請求中的參數(shù)轉(zhuǎn)換成 Handler 方法的參數(shù),今天我們將學(xué)習(xí)另外一種組件 MethodReturnValueHandler,用于將 Handler 返回的數(shù)據(jù)轉(zhuǎn)換為 HTTP 響應(yīng)。
作用
在Spring MVC中,當(dāng)一個控制器方法執(zhí)行完成后,它會返回一個數(shù)據(jù)對象,這個數(shù)據(jù)對象可以是任何類型,如對象、集合、數(shù)組、字符串等等。MethodReturnValueHandler 就是負(fù)責(zé)將這個數(shù)據(jù)對象轉(zhuǎn)換為HTTP響應(yīng)的。
源碼實現(xiàn)
在Spring MVC中,MethodReturnValueHandler 是一個接口,它有多個實現(xiàn)類,每個實現(xiàn)類都可以處理不同類型的數(shù)據(jù)對象。下面是一個簡單的 MethodReturnValueHandler 的實現(xiàn)類示例:
public class MyReturnValueHandler implements MethodReturnValueHandler {
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return returnType.getParameterType().equals(MyData.class);
}
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
MyData myData = (MyData) returnValue;
// do something with myData
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
response.getWriter().write("hello world");
}
}
這個實現(xiàn)類可以將 MyData 類型的數(shù)據(jù)對象轉(zhuǎn)換為 HTTP 響應(yīng)。
supportsReturnType 方法用于判斷該實現(xiàn)類是否支持處理指定類型的返回值。
handleReturnValue 方法用于將數(shù)據(jù)對象轉(zhuǎn)換為 HTTP 響應(yīng)。
在Spring MVC中,MethodReturnValueHandler 由 RequestMappingHandlerAdapter 類負(fù)責(zé)管理。
RequestMappingHandlerAdapter 會維護(hù)一個 MethodReturnValueHandler 列表,當(dāng)控制器方法執(zhí)行完成后,它會遍歷這個列表,找到第一個支持處理當(dāng)前返回值類型的 MethodReturnValueHandler,并調(diào)用它的 handleReturnValue 方法。
public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter {
private final List<MethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
this.returnValueHandlers.add(new MyReturnValueHandler());
}
@Override
public boolean supportsReturnType(MethodParameter returnType) {
for (MethodReturnValueHandler handler : this.returnValueHandlers) {
if (handler.supportsReturnType(returnType)) {
return true;
}
}
return false;
}
@Override
protected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
Object returnValue = handlerMethod.getMethod().invoke(handlerMethod.getBean(), handlerMethod.getMethodParameters());
MethodParameter returnType = handlerMethod.getReturnType();
for (MethodReturnValueHandler handler : this.returnValueHandlers) {
if (handler.supportsReturnType(returnType)) {
ModelAndViewContainer mavContainer = new ModelAndViewContainer();
handler.handleReturnValue(returnValue, returnType, mavContainer, new ServletWebRequest(request, response));
return mavContainer.getModelAndView();
}
}
return null;
}
}在 RequestMappingHandlerAdapter 的 afterPropertiesSet 方法中,我們可以向returnValueHandlers 列表中添加自定義的 MethodReturnValueHandler 實現(xiàn)類。
supportsReturnType 方法用于判斷當(dāng)前返回值類型是否有對應(yīng)的 MethodReturnValueHandler實 現(xiàn)類可以處理。
handleInternal 方法會在控制器方法執(zhí)行完成后調(diào)用,它會根據(jù)返回值類型查找對應(yīng)的 MethodReturnValueHandler 實現(xiàn)類,并調(diào)用它的 handleReturnValue 方法將數(shù)據(jù)對象轉(zhuǎn)換為HTTP響應(yīng)。
總結(jié)
MethodReturnValueHandler 是Spring MVC 中的一個攔截器,它的作用是根據(jù)方法的返回值,將數(shù)據(jù)轉(zhuǎn)換為HTTP響應(yīng)。
在Spring MVC中,MethodReturnValueHandler 由 RequestMappingHandlerAdapter 類負(fù)責(zé)管理。它會維護(hù)一個 MethodReturnValueHandler 列表,在控制器方法執(zhí)行完成后,它會遍歷這個列表,找到第一個支持處理當(dāng)前返回值類型的 MethodReturnValueHandler,并調(diào)用它的handleReturnValue 方法。
我們可以自定義 MethodReturnValueHandler 實現(xiàn)類來處理特定類型的返回值。
到此這篇關(guān)于SpringMVC MethodArgumentResolver的作用與實現(xiàn)的文章就介紹到這了,更多相關(guān)SpringMVC MethodArgumentResolver內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
idea運行tomcat報錯找不到catalina.bat,系統(tǒng)找不到指定的文件問題
這篇文章主要介紹了idea運行tomcat報錯找不到catalina.bat,系統(tǒng)找不到指定的文件問題,具有很好的參考價值,希望對大家有所幫助,2023-11-11
Thymeleaf 3.0 自定義標(biāo)簽方言屬性的實例講解
這篇文章主要介紹了Thymeleaf 3.0 自定義標(biāo)簽方言屬性的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-09-09
SpringBoot?Schedule調(diào)度任務(wù)的動態(tài)管理
Scheduled定時任務(wù)是Spring?boot自身提供的功能,所以不需要引入Maven依賴包,下面這篇文章主要給大家介紹了關(guān)于SpringBoot通過@Scheduled實現(xiàn)定時任務(wù)以及問題解決的相關(guān)資料,需要的朋友可以參考下2023-02-02
SpringBoot中的聲明式事務(wù)+切面事務(wù)+編程式事務(wù)詳解
這篇文章主要介紹了SpringBoot中的聲明式事務(wù)+切面事務(wù)+編程式事務(wù)詳解,事務(wù)管理對于企業(yè)應(yīng)用來說是至關(guān)重要的,當(dāng)出現(xiàn)異常情況時,它也可以保證數(shù)據(jù)的一致性,需要的朋友可以參考下2023-08-08
LeetCode?動態(tài)規(guī)劃之矩陣區(qū)域和詳情
這篇文章主要介紹了LeetCode?動態(tài)規(guī)劃之矩陣區(qū)域和詳情,文章基于Java的相關(guān)資料展開對LeetCode?動態(tài)規(guī)劃的詳細(xì)介紹,需要的小伙伴可以參考一下2022-04-04

