spring設計好美:走入spring請求分發
本編主要是講解,spring請求這整個過程中,以及有哪些地方可以擴充套件
- 1.簡單聊聊springweb
- 2.請求開始
- 2.1 請求全域性變數,RequestContextHolder
- 2.2 監聽ServletRequestHandledEvent事件
- 2.3 配置檔案請求
- 2.4 請求如何找到對應的執行方法handler
- 2.5 使用HandlerInterceptor進行攔截
- 2.6 請求引數是如何獲取的HandlerMethodArgumentResolver
- 2.7 請求引數是如何轉化的?比如@RequestBody
- 2.8 Jackson如何進行訊息轉化的,我們怎麼自定義?HttpMessageConverter
- 2.8 隊請求體進行資料處理?例如加密解密呢?RequestBodyAdvice
- 2.9 請求引數如何校驗處理@Valid
- 2.10 返回值統一處理:HandlerMethodReturnValueHandler
- 2.11 請求體返回值統一處理:ResponseBodyAdvice
1.簡單聊聊springweb
springweb只需要從tomcat如何載入web專案來理解即可。更加簡單一點,tomcat是如何載入Sevlet。
tomcat7之前,我們需要配置web.xml
tomcat啟動之後會去載入我們的web專案,去查詢web.xml。按照web.xml有一定的順序進行載入,並且tomcat會建立ServletContext上下文。
我們web.xml配置了兩個類
org.springframework.web.context.ContextLoaderListener
org.springframework.web.servlet.DispatcherServlet
1.1 兩個核心的Sevlet Api
1.ServletContextListener
容器啟動的時候會去呼叫,ServletContextListener.contextInitialized
ContextLoaderListener就是contextInitialized,容器啟動的時候就會去呼叫contextInitialized,初始化我們的容器。
2.Servlet
tomcat啟動的時候如果設定了sevlet的start等級,或者第一次請求的時候,會初始化sevlet,也就是Servlet.init
而DispatcherServlet也就是Servlet
然後DispatcherServlet就初始化了我們的web容器資訊,檢視,模板等等。
1.2 消失的web.xml
tomcat7之後我們可以不配置web.xml。springboot 以及javaconfig方式都不需要web.xml那是如何啟動的呢?
1.
tomcat7啟動時會有一個方法
ContextConfig,processServletContainerInitializers方法。會去載入javax.servlet.ServletContainerInitializer
並且會去初始化@HandlesTypes註解裡面的資訊。知道這兩個要點就行。
2.
在spring-web的jar檔案中,有一個META-INF/services目錄下javax.servlet.ServletContainerInitializer檔案,這個java的spi機制的都知道,這是java的服務提供發現機制,通過這樣的約束方式,容器會自動找到這個檔案並根據檔案中配置的類名進行例項化
找到了org.springframework.web.SpringServletContainerInitializer
[email protected](WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
我們來看SpringServletContainerInitializer.onStartup
3.WebApplicationInitializer的實現類
AbstractDispatcherServletInitializer
看看它的onStartup方法有個
registerDispatcherServlet,
瞬間豁然開朗,初始化了DispatchSevlet,這樣和我們前面的web.xml方式全部串起來了。
2.請求開始
首先請求是從sevlet開始的,也就是呼叫了sevice方法。
直接看流程圖。
1.service
判斷請求型別,然後呼叫指定方法,以post請求為例。
2.doPost
呼叫了processRequest(request, response);
3.processRequest
加工請求,傳送一些監聽事件
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
LocaleContext localeContext = buildLocaleContext(request);
RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
initContextHolders(request, localeContext, requestAttributes);
try {
doService(request, response);
}
catch (ServletException | IOException ex) {
failureCause = ex;
throw ex;
}
catch (Throwable ex) {
failureCause = ex;
throw new NestedServletException("Request processing failed", ex);
}
finally {
resetContextHolders(request, previousLocaleContext, previousAttributes);
if (requestAttributes != null) {
requestAttributes.requestCompleted();
}
if (logger.isDebugEnabled()) {
if (failureCause != null) {
this.logger.debug("Could not complete request", failureCause);
}
else {
if (asyncManager.isConcurrentHandlingStarted()) {
logger.debug("Leaving response open for concurrent processing");
}
else {
this.logger.debug("Successfully completed request");
}
}
}
publishRequestHandledEvent(request, response, startTime, failureCause);
}
}
2.1 請求全域性變數,RequestContextHolder
從上面程式碼中我們可以看到,請求前,我們初始化了LocaleContextHolder,RequestContextHolder儲存了每次請求的request物件。
那麼我們每次請求,均可以用這個類來獲取request物件。
2.2 監聽ServletRequestHandledEvent事件
從publishRequestHandledEvent(request, response, startTime, failureCause)
public ServletRequestHandledEvent(Object source, String requestUrl,
String clientAddress, String method, String servletName, @Nullable String sessionId,
@Nullable String userName, long processingTimeMillis, @Nullable Throwable failureCause, int statusCode) {
super(source, sessionId, userName, processingTimeMillis, failureCause);
this.requestUrl = requestUrl;
this.clientAddress = clientAddress;
this.method = method;
this.servletName = servletName;
this.statusCode = statusCode;
}
請求結束後傳送了這樣一個預設事件,如果我們在請求後需要用到這個事件,監聽一下就可以了。
繼續往下走
4.doService
對請求添加了一些特殊的引數。並呼叫了doDispatch
5.doDispatch
2.3 配置檔案請求
processedRequest = checkMultipart(request);
protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {
if (WebUtils.getNativeRequest(request, MultipartHttpServletRequest.class) != null) {
logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " +
"this typically results from an additional MultipartFilter in web.xml");
}
else if (hasMultipartException(request) ) {
logger.debug("Multipart resolution failed for current request before - " +
"skipping re-resolution for undisturbed error rendering");
}
else {
try {
return this.multipartResolver.resolveMultipart(request);
}
catch (MultipartException ex) {
if (request.getAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE) != null) {
logger.debug("Multipart resolution failed for error dispatch", ex);
// Keep processing error dispatch with regular request handle below
}
else {
throw ex;
}
}
}
}
// If not returned before: return original request.
return request;
}
我們用@Bean初始化檔案處理方法即可。
例如
@Bean
public MultipartResolver multipartResolver() throws IOException {
CommonsMultipartResolver commonsMultipartResolver = new CommonsMultipartResolver();
commonsMultipartResolver.setUploadTempDir(new FileSystemResource("/temp"));
return commonsMultipartResolver;
}
2.4 請求如何找到對應的執行方法handler
private void addMatchingMappings(Collection<T> mappings, List<Match> matches, HttpServletRequest request) {
for (T mapping : mappings) {
T match = getMatchingMapping(mapping, request);
if (match != null) {
matches.add(new Match(match, this.mappingRegistry.getMappings().get(mapping)));
}
}
}
從我們註冊對映中,取出執行方法
protected HandlerExecutionChain getHandlerExecutionChain(Object handler, HttpServletRequest request) {
HandlerExecutionChain chain = (handler instanceof HandlerExecutionChain ?
(HandlerExecutionChain) handler : new HandlerExecutionChain(handler));
String lookupPath = this.urlPathHelper.getLookupPathForRequest(request);
for (HandlerInterceptor interceptor : this.adaptedInterceptors) {
if (interceptor instanceof MappedInterceptor) {
MappedInterceptor mappedInterceptor = (MappedInterceptor) interceptor;
if (mappedInterceptor.matches(lookupPath, this.pathMatcher)) {
chain.addInterceptor(mappedInterceptor.getInterceptor());
}
}
else {
chain.addInterceptor(interceptor);
}
}
return chain;
}
2.5 使用HandlerInterceptor進行攔截
從上面的程式碼可以看出,我們根據攔截器去判斷是否匹配,匹配的話就加入到我們的HandlerExecutionChain中,這樣稍後就會進行處理。
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = 0; i < interceptors.length; i++) {
HandlerInterceptor interceptor = interceptors[i];
if (!interceptor.preHandle(request, response, this.handler)) {
triggerAfterCompletion(request, response, null);
return false;
}
this.interceptorIndex = i;
}
}
return true;
}
mappedHandler.applyPostHandle(processedRequest, response, mv);
void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv)
throws Exception {
HandlerInterceptor[] interceptors = getInterceptors();
if (!ObjectUtils.isEmpty(interceptors)) {
for (int i = interceptors.length - 1; i >= 0; i--) {
HandlerInterceptor interceptor = interceptors[i];
interceptor.postHandle(request, response, this.handler, mv);
}
}
從程式碼可以看出,如果preHandle返回false,這條請求將不通過,不執行,直接返回
2.6 請求引數是如何獲取的HandlerMethodArgumentResolver
public Object invokeForRequest(NativeWebRequest request, ModelAndViewContainer mavContainer,
Object... providedArgs) throws Exception {
Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
if (logger.isTraceEnabled()) {
logger.trace("Invoking '" + ClassUtils.getQualifiedMethodName(getMethod(), getBeanType()) +
"' with arguments " + Arrays.toString(args));
}
Object returnValue = doInvoke(args);
if (logger.isTraceEnabled()) {
logger.trace("Method [" + ClassUtils.getQualifiedMethodName(getMethod(), getBeanType()) +
"] returned [" + returnValue + "]");
}
return returnValue;
}
這裡是進行引數判斷的
private Object[] getMethodArgumentValues(NativeWebRequest request, ModelAndViewContainer mavContainer,
Object... providedArgs) throws Exception {
MethodParameter[] parameters = getMethodParameters();
Object[] args = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
MethodParameter parameter = parameters[i];
parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
args[i] = resolveProvidedArgument(parameter, providedArgs);
if (args[i] != null) {
continue;
}
if (this.argumentResolvers.supportsParameter(parameter)) {
try {
args[i] = this.argumentResolvers.resolveArgument(
parameter, mavContainer, request, this.dataBinderFactory);
continue;
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug(getArgumentResolutionErrorMessage("Failed to resolve", i), ex);
}
throw ex;
}
}
if (args[i] == null) {
throw new IllegalStateException("Could not resolve method parameter at index " +
parameter.getParameterIndex() + " in " + parameter.getMethod().toGenericString() +
": " + getArgumentResolutionErrorMessage("No suitable resolver for", i));
}
}
return args;
}
最主要的就是argumentResolvers
也就是HandlerMethodArgumentResolverComposite方法的引數校驗HandlerMethodArgumentResolver
HandlerMethodArgumentResolver有兩個方法,一直是否支援,一個是進行處理
寫到這裡,同樣我們也可以模仿進行自定義擴充套件,
這就是針對介面程式設計的魅力
舉兩個例子PathVariableMethodArgumentResolver,也就是我們請求路徑引數@PathVariable
@Override
public boolean supportsParameter(MethodParameter parameter) {
if (!parameter.hasParameterAnnotation(PathVariable.class)) {
return false;
}
if (Map.class.isAssignableFrom(parameter.nestedIfOptional().getNestedParameterType())) {
String paramName = parameter.getParameterAnnotation(PathVariable.class).value();
return StringUtils.hasText(paramName);
}
return true;
}
RequestResponseBodyMethodProcessor,也即是我們
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(RequestBody.class);
}
一目瞭然豁然開朗。
這裡回顧一樣,我們的請求進來,通過HandlerAdapter也就是進行引數判斷,
有很多種引數HandlerMethodArgumentResolver對應不同的處理方法。支援就進行解析,不支援就報錯。
2.7 請求引數是如何轉化的?比如@RequestBody
PathVariableMethodArgumentResolver請求路徑上的
@SuppressWarnings("unchecked")
protected Object resolveName(String name, MethodParameter parameter, NativeWebRequest request) throws Exception {
Map<String, String> uriTemplateVars = (Map<String, String>) request.getAttribute(
HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, RequestAttributes.SCOPE_REQUEST);
return (uriTemplateVars != null ? uriTemplateVars.get(name) : null);
}
@Override
@SuppressWarnings("unchecked")
protected void handleResolvedValue(Object arg, String name, MethodParameter parameter,
ModelAndViewContainer mavContainer, NativeWebRequest request) {
String key = View.PATH_VARIABLES;
int scope = RequestAttributes.SCOPE_REQUEST;
Map<String, Object> pathVars = (Map<String, Object>) request.getAttribute(key, scope);
if (pathVars == null) {
pathVars = new HashMap<String, Object>();
request.setAttribute(key, pathVars, scope);
}
pathVars.put(name, arg);
}
也就是把請求的路徑的引數獲取出來,然後賦予給方法的引數值上。
同理:RequestResponseBodyMethodProcessor
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
parameter = parameter.nestedIfOptional();
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());
String name = Conventions.getVariableNameForParameter(parameter);
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
return adaptArgumentIfNecessary(arg, parameter);
}
2.8 Jackson如何進行訊息轉化的,我們怎麼自定義?HttpMessageConverter
從上面的Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());
從程式碼一步步走
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {
MediaType contentType;
boolean noContentType = false;
try {
contentType = inputMessage.getHeaders().getContentType();
}
catch (InvalidMediaTypeException ex) {
throw new HttpMediaTypeNotSupportedException(ex.getMessage());
}
if (contentType == null) {
noContentType = true;
contentType = MediaType.APPLICATION_OCTET_STREAM;
}
Class<?> contextClass = (parameter != null ? parameter.getContainingClass() : null);
Class<T> targetClass = (targetType instanceof Class ? (Class<T>) targetType : null);
if (targetClass == null) {
ResolvableType resolvableType = (parameter != null ?
ResolvableType.forMethodParameter(parameter) : ResolvableType.forType(targetType));
targetClass = (Class<T>) resolvableType.resolve();
}
HttpMethod httpMethod = ((HttpRequest) inputMessage).getMethod();
Object body = NO_VALUE;
try {
inputMessage = new EmptyBodyCheckingHttpInputMessage(inputMessage);
for (HttpMessageConverter<?> converter : this.messageConverters) {
Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass();
if (converter instanceof GenericHttpMessageConverter) {
GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter;
if (genericConverter.canRead(targetType, contextClass, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
}
if (inputMessage.getBody() != null) {
inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType);
body = genericConverter.read(targetType, contextClass, inputMessage);
body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);
}
else {
body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType);
}
break;
}
}
else if (targetClass != null) {
if (converter.canRead(targetClass, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
}
if (inputMessage.getBody() != null) {
inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType);
body = ((HttpMessageConverter<T>) converter).read(targetClass, inputMessage);
body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);
}
else {
body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType);
}
break;
}
}
}
}
catch (IOException ex) {
throw new HttpMessageNotReadableException("I/O error while reading input message", ex);
}
if (body == NO_VALUE) {
if (httpMethod == null || !SUPPORTED_METHODS.contains(httpMethod) ||
(noContentType && inputMessage.getBody() == null)) {
return null;
}
throw new HttpMediaTypeNotSupportedException(contentType, this.allSupportedMediaTypes);
}
return body;
}
這是核心程式碼慢慢來分析。
1.獲得contentType請求型別
2.獲取請求的介面以及引數型別
3.遍歷messageConverters進行判斷
4.判斷是不是GenericHttpMessageConverter型別的訊息轉化器,如果不是並且引數類不為空就呼叫HttpMessageConverter.canRead判斷能否讀,
例如如果你是物件,這裡是一個String轉化器當然不能read。
拿Jackson來舉例
class org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
這是轉化類它是GenericHttpMessageConverter
@Override
public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
if (!canRead(mediaType)) {
return false;
}
JavaType javaType = getJavaType(type, contextClass);
AtomicReference<Throwable> causeRef = new AtomicReference<Throwable>();
if (this.objectMapper.canDeserialize(javaType, causeRef)) {
return true;
}
logWarningIfNecessary(javaType, causeRef.get());
return false;
}
首先判斷支援的型別,application/json,然後判斷這裡的請求引數類是否可以用jackson進行解析
if (genericConverter.canRead(targetType, contextClass, contentType)) {
if (logger.isDebugEnabled()) {
logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]");
}
if (inputMessage.getBody() != null) {
inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType);
body = genericConverter.read(targetType, contextClass, inputMessage);
body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);
}
else {
body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType);
}
break;
}
如果可以
1.先取出請求體
2.再用jackson讀出物件
這裡有兩個
getAdvice().beforeBodyRead
getAdvice().afterBodyRead
這是一個擴充套件點,稍後講解
@Override
public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
throws IOException, HttpMessageNotReadableException {
JavaType javaType = getJavaType(type, contextClass);
return readJavaType(javaType, inputMessage);
}
private Object readJavaType(JavaType javaType, HttpInputMessage inputMessage) {
try {
if (inputMessage instanceof MappingJacksonInputMessage) {
Class<?> deserializationView = ((MappingJacksonInputMessage) inputMessage).getDeserializationView();
if (deserializationView != null) {
return this.objectMapper.readerWithView(deserializationView).forType(javaType).
readValue(inputMessage.getBody());
}
}
return this.objectMapper.readValue(inputMessage.getBody(), javaType);
}
catch (JsonProcessingException ex) {
throw new HttpMessageNotReadableException("JSON parse error: " + ex.getOriginalMessage(), ex);
}
catch (IOException ex) {
throw new HttpMessageNotReadableException("I/O error while reading input message", ex);
}
}
這裡就用jackson讀出瞭解析出了物件。
2.8 隊請求體進行資料處理?例如加密解密呢?RequestBodyAdvice
我們看到用jackson進行處理的時候
inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType);
body = genericConverter.read(targetType, contextClass, inputMessage);
body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType);
@Override
public HttpInputMessage beforeBodyRead(HttpInputMessage request, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
for (RequestBodyAdvice advice : getMatchingAdvice(parameter, RequestBodyAdvice.class)) {
if (advice.supports(parameter, targetType, converterType)) {
request = advice.beforeBodyRead(request, parameter, targetType, converterType);
}
}
return request;
}
@Override
public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
for (RequestBodyAdvice advice : getMatchingAdvice(parameter, RequestBodyAdvice.class)) {
if (advice.supports(parameter, targetType, converterType)) {
body = advice.afterBodyRead(body, inputMessage, parameter, targetType, converterType);
}
}
return body;
}
你們看錯就是它,RequestBodyAdvice
它有四個方法
supports:是否支援,自己做處理,比如過濾等等
handleEmptyBody:空的body如何處理
beforeBodyRead:請求體預處理
afterBodyRead:
如何做加密解密
beforeBodyRead對請求體做解密,
afterBodyRead
2.9 請求引數如何校驗處理@Valid
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
parameter = parameter.nestedIfOptional();
Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());
String name = Conventions.getVariableNameForParameter(parameter);
WebDataBinder binder = binderFactory.createBinder(webRequest, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
return adaptArgumentIfNecessary(arg, parameter);
}
protected void validateIfApplicable(WebDataBinder binder, MethodParameter parameter) {
Annotation[] annotations = parameter.getParameterAnnotations();
for (Annotation ann : annotations) {
Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] {hints});
binder.validate(validationHints);
break;
}
}
}
判斷如果有Valid 就進行驗證,這裡用的WebMvcValidator.
接下來無非就是取出所有欄位,反射取出驗證條件,進行判斷,如果驗證不過,就放入錯誤中。
public void invokeAndHandle(ServletWebRequest webRequest, ModelAndViewContainer mavContainer,
Object... providedArgs) throws Exception {
Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);
setResponseStatus(webRequest);
if (returnValue == null) {
if (isRequestNotModified(webRequest) || getResponseStatus() != null || mavContainer.isRequestHandled()) {
mavContainer.setRequestHandled(true);
return;
}
}
else if (StringUtils.hasText(getResponseStatusReason())) {
mavContainer.setRequestHandled(true);
return;
}
mavContainer.setRequestHandled(false);
try {
this.returnValueHandlers.handleReturnValue(
returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
}
catch (Exception ex) {
if (logger.isTraceEnabled()) {
logger.trace(getReturnValueHandlingErrorMessage("Error handling return value", returnValue), ex);
}
throw ex;
}
}
this.returnValueHandlers.handleReturnValue(
returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
返回值處理
2.10 返回值統一處理:HandlerMethodReturnValueHandler
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
HandlerMethodReturnValueHandler handler = selectHandler(returnValue, returnType);
if (handler == null) {
throw new IllegalArgumentException("Unknown return value type: " + returnType.getParameterType().getName());
}
handler.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
}
private HandlerMethodReturnValueHandler selectHandler(Object value, MethodParameter returnType) {
boolean isAsyncValue = isAsyncReturnValue(value, returnType);
for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {
if (isAsyncValue && !(handler instanceof AsyncHandlerMethodReturnValueHandler)) {
continue;
}
if (handler.supportsReturnType(returnType)) {
return handler;
}
}
return null;
}
同樣先判斷是否支援
例如RequestResponseBodyMethodProcessor
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) ||
returnType.hasMethodAnnotation(ResponseBody.class));
}
判斷方法是否有ResponseBody有才用這個處理。
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest)
throws IOException, HttpMediaTypeNotAcceptableException, HttpMessageNotWritableException {
mavContainer.setRequestHandled(true);
ServletServerHttpRequest inputMessage = createInputMessage(webRequest);
ServletServerHttpResponse outputMessage = createOutputMessage(webRequest);
// Try even with null return value. ResponseBodyAdvice could get involved.
writeWithMessageConverters(returnValue, returnType, inputMessage, outputMessage);
}
writeWithMessageConverters和請求的時候類似。
1.取出響應返回值型別,
2.進行訊息轉化型別判斷,
3.是否可以寫,可以寫就寫,
如果我們要自定義返回響應體就可以模仿@ResponseBody自己寫一個類似的,實現HandlerMethodReturnValueHandler即可。
2.11 請求體返回值統一處理:ResponseBodyAdvice
if (((GenericHttpMessageConverter) messageConverter).canWrite(
declaredType, valueType, selectedMediaType)) {
outputValue = (T) getAdvice().beforeBodyWrite(outputValue, returnType, selectedMediaType,
(Class<? extends HttpMessageConverter<?>>) messageConverter.getClass(),
inputMessage, outputMessage);
if (outputValue != null) {
addContentDispositionHeader(inputMessage, outputMessage);
((GenericHttpMessageConverter) messageConverter).write(
outputValue, declaredType, selectedMediaType, outputMessage);
if (logger.isDebugEnabled()) {
logger.debug("Written [" + outputValue + "] as \"" + selectedMediaType +
"\" using [" + messageConverter + "]");
}
}
return;
}
和請求體統一處理一直,
這裡就可以做加密處理了
響應體處理完後,直接outputstream輸出到返回體中。
接下來就是其他檢視的轉化的,比如jsp等等。這個就不往下研究了。
springweb的核心就是sevlet,萬變不離其中,就如spring生命週期一樣,spring提供了很多額外的擴充套件給我們使用,比如攔截器,RequestBodyAdviceAdapter,ResponseBodyAdvice,訊息轉化器等等。
只要抓住了核心,不管它怎麼變化,都是小菜一碟。