Spring消息转换流程

2021-07-23  本文已影响0人  十毛tenmao

Spring MVC消息转换流程

1. 调用方法获取返回值

org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod#invokeAndHandle

Object returnValue = invokeForRequest(webRequest, mavContainer, providedArgs);

2. 获取返回值的处理器

org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite#handleReturnValue

//如果使用了@RequestBody,返回的就是RequestResponseBodyMethodProcessor
HandlerMethodReturnValueHandler handler = selectHandler(returnValue, returnType);

3. 使用MessageConverter转换body

org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor#handleReturnValue

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

消息转换的逻辑

上一篇下一篇

猜你喜欢

热点阅读