1.问题提出
在SpringBoot开发Web应用的过程中,实际上底层还是使用的是SpringMVC的实现,当用户发起请求,被前端控制器DispatcherServlet所拦截到,进行一系列的处理,返回给用户数据, 它是怎么处理返回结果的呢?
2.原理分析
查看DispatcherServlet的继承结构
通过继承树我们可以发现,该类其实是一个Servlet,SpringMVC内部进行了层层的封装,既然是一个Servlet,那么就肯定会实现doGet和doPost方法,在其抽象父类中进行了实现
我们可以看到FrameworkServlet实现这两个方法,内部都调用了processRequest();方法,我们查看processRequest();的处理逻辑。
核心调用方法为doService();点进去看其实现
来看DispatcherServlet对此方法的实现
直接上源码
@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<String, O