springboot学习12

1、SpringWebMVC REST
资源操作

HTTP :GET、PUT、POST、DELETE

定义:

注解 说明 Spring Framework版本
@Controller 应用控制器注解声明,Spring 模式注解 2.5 +
@RestController 等效于 @Controller + @ResponseBody 4.0 +

映射:

注解 说明 Spring Framework版本
@RequestMapping 应用控制器映射注解声明 2.5 +
@GetMapping GET 方法映射,等效于 @RequestMapping(method = RequestMethod.GET) 4.3 +
@PostMapping POST 方法映射,等效于 @RequestMapping(method = RequestMethod.POST) 4.3 +
@PutMapping PUT 方法映射,等效于 @RequestMapping(method = RequestMethod.PUT) 4.3 +
@DeleteMapping DELETE 方法映射,等效于 @RequestMapping(method = RequestMethod.DELETE) 4.3 +
@GetMapping GET 方法映射,等效于 @RequestMapping(method = RequestMethod.GET) 4.3 +
@PatchMapping PATCH 方法映射,等效于 @RequestMapping(method = RequestMethod.PATCH) 4.3 +

请求:

注解 说明 Spring Framework 版本
@RequestParam 获取请求参数 2.5 +
@RequestHeader 获取请求头 3.0 +
@CookieValue 获取Cookie值 3.0 +
@RequestBody 获取完整请求主体内容 3.0 +
@PathVariable 获取请求路径变量 3.0 +
RequestEntity 获取请求内容(包括请求主体和请求头) 4.1 +

拦截:

注解 说明 Spring Framework 版本
@RestControllerAdvice @RestController 注解切面通知 4.3 +
HandlerInterceptor 处理方法拦截器 1.0

跨域:

注解 说明 Spring Framework 版本
@CrossOrigin 资源跨域声明注解 4.2 +
CorsFilter 资源跨域拦截器 4.2 +
WebMvcConfigurer#addCorsMappings 注册资源跨域信息 4.2 +

2、主要组件

组件名称 实现 说明
内容协商管理器 ContentNegotiationManager ContentNegotiationStrategy 控制策略
媒体类型 MediaType HTTP 消息媒体类型,如 text/html
消费媒体类型 @RequestMapping#consumes 请求头 Content-Type 媒体类型映射
生产媒体类型 @RequestMapping#produces 响应头 Content-Type 媒体类型映射
HTTP消息转换器 HttpMessageConverter HTTP 消息转换器,用于反序列化 HTTP 请求或序列化响应
Web MVC 配置器 WebMvcConfigurer 配置 REST 相关的组件
处理方法 HandlerMethod @RequestMapping 标注的方法
处理方法参数解析器 HandlerMethodArgumentResolver 用于 HTTP 请求中解析 HandlerMethod 参数内容
处理方法返回值解析器 HandlerMethodReturnValueHandler 用于 HandlerMethod 返回值解析为 HTTP 响应内容

3、SpringWebMVC REST 处理流程

1、Client -- REST--> DispatcherServlet
2、DispatcherServlet -- 寻找@RequestMappingHandlerMethod-->RequestMappingHandlerMethod
3、RequestMappingHandlerMethod-- 关联HandlerMethod-->@Controller
4、DispatcherServlet --根据HandlerMethod寻找HandlerAdapter -->RequestMappingHandlerAdapter 
5、RequestMappingHandlerAdapter -- HandlerMethod转换-->InvocableHandlerMethod
6、InvocableHandlerMethod -- 处理请求 -- >HandlerMethodArgumentResolver
7、HandlerMethodArgumentResolver-- 解析方法参数--> @Controller
8、@Controller -- HandlerMethod 执行结果--> InvocableHandlerMethod
9、InvocableHandlerMethod -- HandlerMethod 执行结果 -->HandlerMethodReturnValueHandler
10、HandlerMethodReturnValueHandler -- 转化Http消息--> (RequestResponseBodyMethodProcessor、HttpEntityMethodProcessor、...)
11、(RequestResponseBodyMethodProcessor、HttpEntityMethodProcessor、...) -- 返回REST HTTP消息(JSON、XML等)--> Client

猜你喜欢

转载自blog.csdn.net/tongwudi5093/article/details/113762238