既是一个参数解析器,也是一个返回结果处理器。
1.持有消息转换器的集合
protected final List> messageConverters;
2.作为参数解析器,例如对@RequestBody标识的参数进行解析
@Override
public boolean supportsParameter(MethodParameter parameter) {return parameter.hasParameterAnnotation(RequestBody.class);
}
@Override
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {parameter = parameter.nestedIfOptional();//核心的参数解析Object arg = readWithMessageConverters(webRequest, parameter, parameter.getNestedGenericParameterType());//...return adaptArgumentIfNecessary(arg, parameter);
}
readWithMessageConverters()函数会将HTTP请求体中的JSON转为我们的Java对象,主要用到转换器的**canRead()和Read()**方法,其源码如下:
@Nullable
protected Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter,Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException {//拿到请求类型MediaType contentType;boolean noContentType = false;try {contentType = inputMessage.getHeaders().getContentType();}//...//拿到我们的参数类型和Controller类型Class> contextClass = parameter.getContainingClass();Class targetClass = (targetType instanceof Class ? (Class) targetType : null);//...//拿到HTTP请求类型HttpMethod httpMethod = (inputMessage instanceof HttpRequest ? ((HttpRequest) inputMessage).getMethod() : null);Object body = NO_VALUE;EmptyBodyCheckingHttpInputMessage message = null;try {message = new EmptyBodyCheckingHttpInputMessage(inputMessage);//开始遍历所有的HttpMessageConverter,看看谁支持将当前http请求内容转为我们的参数for (HttpMessageConverter> converter : this.messageConverters) {Class> converterType = (Class>) converter.getClass();GenericHttpMessageConverter> genericConverter =(converter instanceof GenericHttpMessageConverter ? (GenericHttpMessageConverter>) converter : null);if (genericConverter != null ? genericConverter.canRead(targetType, contextClass, contentType) ://核心就是canRead,与我们参数解析器的support()功能一致(targetClass != null && converter.canRead(targetClass, contentType))) {if (message.hasBody()) {HttpInputMessage msgToUse =getAdvice().beforeBodyRead(message, parameter, targetType, converterType);//找到HttpMessageConverter后调用read方法进行转化body = (genericConverter != null ? genericConverter.read(targetType, contextClass, msgToUse) :((HttpMessageConverter) converter).read(targetClass, msgToUse));body = getAdvice().afterBodyRead(body, msgToUse, parameter, targetType, converterType);}else {body = getAdvice().handleEmptyBody(null, message, parameter, targetType, converterType);}break;}}}//...异常捕捉与日志记录return body;
}
3.作为返回值处理器,可以处理@ResponseeBody所标识方法的返回值
this.returnValueHandlers.handleReturnValue(returnValue, getReturnValueType(returnValue), mavContainer, webRequest);
public void handleReturnValue(@Nullable 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(@Nullable 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;
}
supportsReturnType()是判断是否支持当前返回值,源码如下:
@Override
public boolean supportsReturnType(MethodParameter returnType) {return (AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ResponseBody.class) ||returnType.hasMethodAnnotation(ResponseBody.class));
}