教你SpringMVC处理请求的方式
2021-03-18 12:28
标签:map cond eve 它的 获得 ext getname 入口 filter 先上图: Spring MVC 框架也是一个基于请求驱动的 Web 框架,并且使用了前端控制器模式(是用来提供一个集中的请求处理机制,所有的请求都将由一个单一的处理程序处理来进行设计,再根据请求映射规则分发给相应的页面控制器(动作/处理器)进行处理。首先让我们整体看一下 Spring MVC 处理请求的流程: 首先用户发送请求,请求被 SpringMVC前端控制器(DispatherServlet)捕获; 前端控制器(DispatherServlet)对请求 URL 解析获取请求 URI,根据 URI,调用 HandlerMapping; 前端控制器(DispatherServlet)获得返回的 HandlerExecutionChain(包括 Handler 对象以及 Handler 对象对应的拦截器); DispatcherServlet 根据获得的 HandlerExecutionChain,选择一个合适的 HandlerAdapter。(附注:如果成功获得 HandlerAdapter 后,此时将开始执行拦截器的 preHandler(...) 方法); HandlerAdapter 根据请求的 Handler 适配并执行对应的 Handler;HandlerAdapter 提取 Request 中的模型数据,填充 Handler 入参,开始执行 Handler(Controller)。 在填充 Handler 的入参过程中,根据配置,Spring 将做一些额外的工作: HttpMessageConveter:将请求消息(如 Json、xml 等数据)转换成一个对象,将对象转换为指定的响应信息; 数据转换:对请求消息进行数据转换。如 String 转换成 Integer、Double 等; 数据格式化:如将字符串转换成格式化数字或格式化日期等; 数据验证: 验证数据的有效性(长度、格式等),验证结果存储到 BindingResult 或 Error 中); Handler 执行完毕,返回一个 ModelAndView (即模型和视图)给 HandlerAdaptor; HandlerAdaptor 适配器将执行结果 ModelAndView 返回给前端控制器; 前端控制器接收到 ModelAndView 后,请求对应的视图解析器; 视图解析器解析 ModelAndView 后返回对应 View; 渲染视图并返回渲染后的视图给前端控制器; 对于 SpringMVC 项目所有的请求入口(静态资源除外)这里都是从 web.xml 文件配置的前端控制器 DispatcherServlet 开始: DispatcherServlet UML继承关系图如下: 这里关注蓝线部分继承结构:DispatcherServlet-->FrameworkServlet-->HttpServletBean-->HttpServlet-->GenericServlet-->Servlet,对于请求核心时序图如下: 对于 web 请求的处理,大家都知道是通过继承 HttpServlet 重写其 service 方法,这里打开 DispatcherServlet 源码发现这里并没有看到我们要找的 service 方法,此时到父类 FrameworkServlet 查找如下:可以看到父类重写 HttpServlet service 方法。 从源码分析来看当请求方法为 patch 请求或者为 null 时执行 proce***equest0 方法,其他情况则调用父类 service 方法,大家都知道 SpringMVC 请求大多请求是 get|post 请求为主,此时继续向上查看 FrameworkServlet 父类 HttpServletBean(抽象类继承 HttpServlet 并未重写 service 方法,所以向上继续寻找)--> HttpServlet service 方法: 可以看到 HttpServlet service 进行了重载,根据不同的请求类型然后调用不同处理方法,这里以 get 请求为例,当请求方法为 get 请求时在重载 service 方法中调用 doGet 方法进行处理,这里需要特别注意的是:HttpServlet 存在 doGet 方法实现,然而在继承的子类中也存在 doGet 方法实现,到底调用哪个方法?很明显调用子类的 doGet 方法(面向对象多态思想!!!)从继承 UML 关系图上看,最外层子类实现 doGet 方法的为 FrameworkServlet : 该方法大概做了这几件事:国际化的设置,创建 ServletRequestAttributes 对象,初始化上下文 holders (即将 Request 对象放入到线程上下文中,如后续想要在方法中获取 request、response对象此时可以通过调用 LocaleContextHolder 对应方法即可),然后调用 doService 方法。对于 doService 方法,FrameworkServlet 类并未提供实现,该方法由 DispatcherServlet 子类实现: DispatcherServlet 里面执行处理的入口方法是 doService,由于这个类继承于 FrameworkServlet 类,重写了 doService() 方法: 整个方法看下来处理的操作有:处理 include 标签的请求,将上下文放到 request 的属性中,将国际化解析器放到 request 的属性中,将主题解析器放到 request 属性中,将主题放到 request 的属性中,处理重定向的请求数据最后调用 doDispatch 这个核心的方法对请求进行处理: 该方法是在 doService 方法中调用的,从底层设计了整个请求的处理流程: doDispatcher 首先检查是不是上传请求,如果是则将 request 转换为 MultipartHttpServletRequest,并将 multipartRequestParsed 标志设置为 true; processDispatchResult 方法主要用来处理前面返回的结果,其中包括处理异常、渲染页面、触发 Interceptor 的 afterCompletion 方法三部分内容,处理的异常是在处理请求 doDispatch 方法的过程中产生。 render 视图渲染: 今天我们来了解了一下 SpringMVC 框架中 MVC 核心思想,SpringMVC 内部请求流程分析以及源码级别代码解读,让大家真正能够从底层级别理解整个框架执行原貌,最后以一张图来总结今天的源码分析执行流程。 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计思想。它主要通过分离模型、视图及控制器在应用程序中的角色将业务逻辑从界面中解耦。通常,模型负责封装应用程序数据在视图层展示。视图仅仅只是展示这些数据,不包含任何业务逻辑。控制器负责接收来自用户的请求,并调用后台服务(service或者dao)来处理业务逻辑。处理后,后台业务层可能会返回了一些数据在视图层展示。控制器收集这些数据及准备模型在视图层展示。MVC模式的核心思想是将业务逻辑从界面中分离出来,允许它们单独改变而不会相互影响。 教你SpringMVC处理请求的方式 标签:map cond eve 它的 获得 ext getname 入口 filter 原文地址:https://blog.51cto.com/14819669/2541655请求流程的方式
案例实操
SpringMVC 请求执行源码解读
FrameworkServlet#service
/**
* Override the parent class implementation in order to intercept PATCH requests.
*/
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpMethod httpMethod = HttpMethod.resolve(request.getMethod());
if (httpMethod == HttpMethod.PATCH || httpMethod == null) {
proce***equest(request, response);
}
else {
super.service(request, response);
}
}
HttpServlet#service
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
if (!(req instanceof HttpServletRequest &&
res instanceof HttpServletResponse)) {
throw new ServletException("non-HTTP request or response");
}
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
service(request, response);
}
}
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn‘t support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince
FrameworkServlet#doGet&proce***equest
@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
proce***equest(request, response);
}
protected final void proce***equest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 系统计时开始时间
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
// 国际化
LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
LocaleContext localeContext = buildLocaleContext(request);
//构建ServletRequestAttributes对象
RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
//异步管理
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
//初始化ContextHolders
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 {
//恢复原来的LocaleContext和ServiceRequestAttributes到LocaleContextHolder和RequestContextHolder,避免影响Servlet以外的处理,如Filter
resetContextHolders(request, previousLocaleContext, previousAttributes);
if (requestAttributes != null) {
requestAttributes.requestCompleted();
}
logResult(request, response, failureCause, asyncManager);
//发布ServletRequestHandlerEvent消息,这个请求是否执行成功都会发布消息的
publishRequestHandledEvent(request, response, startTime, failureCause);
}
}
// initContextHolders(request, localeContext, requestAttributes);
private void initContextHolders(HttpServletRequest request,
@Nullable LocaleContext localeContext, @Nullable RequestAttributes requestAttributes) {
if (localeContext != null) {
LocaleContextHolder.setLocaleContext(localeContext, this.threadContextInheritable);
}
if (requestAttributes != null) {
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
}
}
DispatcherServlet#doService
@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
logRequest(request);
// Keep a snapshot of the request attributes in case of an include,
// to be able to restore the original attributes after the include.
Map
DispatcherServlet#doDispatch
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
// 校验是否为上传请求 是上传请求执行解析 否则返回request
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// 根据访问的Handler 返回指定对应的HandlerExecutionChain对象 这里从HandlerMapping 集合中查找 HandlerExecutionChain 对象包含Handler与拦截器HandlerInterceptor列表
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// 根据得到的Handler 获取对应的HandlerAdaptor对象
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// 处理GET、HEAD请求的Last-Modified
String method = request.getMethod();
boolean isGet = "GET".equals(method);
if (isGet || "HEAD".equals(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
//当数据没有更改时,就直接返回上次的数据,提高效率
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
//执行Interceptor的preHandle
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// 执行Handler 返回ModelAndView
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
//如果需要异步处理,直接返回
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
//当view为空时,根据request设置默认view,如Handler返回值为void
applyDefaultViewName(processedRequest, mv);
//执行相应Interceptor的postHandle
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we‘re processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new NestedServletException("Handler dispatch failed", err);
}
//处理返回结果,包括处理异常、渲染页面,发出完成通知触发Interceptor的afterCompletion
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new NestedServletException("Handler processing failed", err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
DispatcherServlet#processDispatchResult
private void processDispatchResult(HttpServletRequest request, HttpServletResponse response,
@Nullable HandlerExecutionChain mappedHandler, @Nullable ModelAndView mv,
@Nullable Exception exception) throws Exception {
boolean errorView = false;
// 如果请求过程中有异常抛出则处理异常
if (exception != null) {
if (exception instanceof ModelAndViewDefiningException) {
logger.debug("ModelAndViewDefiningException encountered", exception);
mv = ((ModelAndViewDefiningException) exception).getModelAndView();
}
else {
Object handler = (mappedHandler != null ? mappedHandler.getHandler() : null);
mv = processHandlerException(request, response, handler, exception);
errorView = (mv != null);
}
}
//执行页面渲染操作
if (mv != null && !mv.wasCleared()) {
render(mv, request, response);
if (errorView) {
WebUtils.clearErrorRequestAttributes(request);
}
}
else {
if (logger.isTraceEnabled()) {
logger.trace("No view rendering, null ModelAndView returned.");
}
}
if (WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
// Concurrent handling started during a forward
return;
}
// Handler请求处理完,触发Interceptor的afterCompletion
if (mappedHandler != null) {
// Exception (if any) is already handled..
mappedHandler.triggerAfterCompletion(request, response, null);
}
}
protected void render(ModelAndView mv, HttpServletRequest request, HttpServletResponse response) throws Exception {
// Determine locale for request and apply it to the response.
Locale locale =
(this.localeResolver != null ? this.localeResolver.resolveLocale(request) : request.getLocale());
response.setLocale(locale);
View view;
String viewName = mv.getViewName();
if (viewName != null) {
// We need to resolve the view name.
view = resolveViewName(viewName, mv.getModelInternal(), locale, request);
if (view == null) {
throw new ServletException("Could not resolve view with name ‘" + mv.getViewName() +
"‘ in servlet with name ‘" + getServletName() + "‘");
}
}
else {
// No need to lookup: the ModelAndView object contains the actual View object.
view = mv.getView();
if (view == null) {
throw new ServletException("ModelAndView [" + mv + "] neither contains a view name nor a " +
"View object in servlet with name ‘" + getServletName() + "‘");
}
}
if (logger.isTraceEnabled()) {
logger.trace("Rendering view [" + view + "] ");
}
try {
if (mv.getStatus() != null) {
response.setStatus(mv.getStatus().value());
}
// 渲染页面处理
view.render(mv.getModelInternal(), request, response);
}
catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Error rendering view [" + view + "]", ex);
}
throw ex;
}
}
扩展~MVC