Spring MVC execution process (detailed explanation) + common annotations (detailed explanation) + knowledge points

1. Processing flow: (abbreviation + detailed explanation)


/**abbreviation*/
1. The specified request sent by the browser will be handed over to DispatcherServlet (front-end controller), 
2. DispatcherServlet Call HandlerMapping (processor mapper) to find and return the Handler (processor) corresponding to the request path.
3. DispatcherServlet calls HandlerAdapter (processor adapter), and HandlerAdapter calls the specific processor (Controller, also called back-end controller) through adaptation.
4. After the target Handler (Controller) finishes processing the business, it will return a ModelAndView (model and view), and then the DispatcherServlet calls the ViewResolver (view resolver) to determine the specific view component and render the view. 
5. Finally return to the client

/**Detailed writing*/    
<Complex version>
1. The user sends a request to the front-end controller DispatcherServlet.
2. DispatcherServlet receives the request and calls the HandlerMapping processor mapper.
3. The processor mapper finds the specific processor (can be searched according to xml configuration and annotations), generates the processor object and processor interceptor (if any) and returns them to DispatcherServlet.
4. DispatcherServlet calls HandlerAdapter processor adapter.
5. HandlerAdapter calls the specific processor (Controller, also called back-end controller) through adaptation.
6. The Controller returns to ModelAndView after execution is completed.
7. HandlerAdapter returns the controller execution result ModelAndView to DispatcherServlet.
8. DispatcherServlet passes the ModelAndView to the ViewReslover view parser.
9. ViewReslover returns the specific View after parsing.
10. DispatcherServlet renders the view based on the View (that is, fills the model data into the view).
11. DispatcherServlet responds to the user.

/**2.1核心组件*/
DispatcherServlet:前置控制器,负责调度其他组件的执行,可以降低不同组件之间的耦合性,是整个Spring MVC的核心模块
Handler:处理器,完成具体的业务逻辑,相当于Servlet
HandlerMapping:DispatcherServlet是通过 HandlerMapping把请求映射到不同的Handler

HandlerAdapter(适配器的作用大致就是,将请求消息(如Json,xml等数据)转换成一个对象,将对象转换为指定的响应信息) (二阶段手写的框架,解析请求信息,将其拆分处理,该转换的转换该提取的提取该验证的验证,最后把能用的有效数据传到目标Controller中,让其进行业务处理)
ModelAndView:封装了模型数据和视图信息,作为Handler的处理结果,返回给DispatcherServlet
ViewResolver:视图解析器,DispatcherServlet通过它把逻辑视图解析为物理视图,最终把渲染的结果响应给客户端

[Illustration (simplified version)]

 [Illustration (detailed version)]

2.Spring MVC common annotations

 

1、@RequestMapping   (配置请求路径注解)(是配置请求路径与处理请求的方法的映射关系)
   @PostMapping      (增删改数据居多)
   @GetMapping       (查询数据居多)
    /*例子:添加在方法上,可以什么也不写*/
     @GetMapping("")
    public JsonResult list() {}
    
2.@Controller(组件扫描注解)
  @RestController(组件扫描+响应注解) = @Controller + @ResponseBody 
  @ResponseBody:一旦配置了@ResponseBody,此类处理请求的方法的返回值就会直接响应到客户端去    
    /*例子:在类上加注解*/
    @RestController
	@RequestMapping("/brands")
	public class BrandController {}
        
3.@RequestBody和@ResponsBody     传输json 格式数据
    /*例子:在参数上加注解*/
     @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody BrandAddNewDTO brandAddNewDTO) {}

4、@PathVariable    当路径URL需要正则表达式判断时添加   (可替代@RequestBody) 
    /*例子:在参数上加注解*/
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id) {}

5.自动装配
    @Autowired @Resource

3. Overall example (code)

/**总体例子:*/
/**
 * 相册控制器
 *
 * @author [email protected]
 * @version 0.0.1
 */
@Slf4j                       //日志
@Api(tags = "3. 相册管理模块") //knife4j测试器(测试用的,可以不加)
@RestController
@RequestMapping("/albums")
public class AlbumController {    //Controller控制层代码  (添加,删除,查询业务)

    @Autowired //自动装配
    private IAlbumService albumService;

    public AlbumController() {        
        log.debug("创建控制器对象:AlbumController"); 
    }

    // http://localhost:9080/albums/add-new
    @ApiOperation("添加相册")           //knife4j测试器
    @ApiOperationSupport(order = 100)  //knife4j测试器
    @PostMapping("/add-new")
    public JsonResult addNew(@RequestBody AlbumAddNewDTO albumAddNewDTO) {
        log.debug("接收到的请求参数:{}", albumAddNewDTO);
        albumService.addNew(albumAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums/1/delete
    @ApiOperation("删除相册")             //knife4j测试器
    @ApiOperationSupport(order = 200)    //knife4j测试器
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult deleteById(@PathVariable Long id) {
        log.debug("接收到的请求参数:{}", id);
        albumService.deleteById(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/albums
    @ApiOperation("查询相册列表")          //knife4j测试器
    @ApiOperationSupport(order = 400)    //knife4j测试器
    @GetMapping("")
    public JsonResult list() {
        log.debug("接收到查询相册列表的请求");
        List<AlbumListItemVO> albums = albumService.list();
        return JsonResult.ok(albums);
    }

}

Guess you like

Origin blog.csdn.net/m0_71202849/article/details/126857321