springboot学习12

1、SpringWebMVC REST
资源操作

HTTP :GET、PUT、POST、DELETE

定义:

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

映射:

注解说明Spring Framework版本
@RequestMapping应用控制器映射注解声明2.5 +
@GetMappingGET 方法映射,等效于 @RequestMapping(method = RequestMethod.GET)4.3 +
@PostMappingPOST 方法映射,等效于 @RequestMapping(method = RequestMethod.POST)4.3 +
@PutMappingPUT 方法映射,等效于 @RequestMapping(method = RequestMethod.PUT)4.3 +
@DeleteMappingDELETE 方法映射,等效于 @RequestMapping(method = RequestMethod.DELETE)4.3 +
@GetMappingGET 方法映射,等效于 @RequestMapping(method = RequestMethod.GET)4.3 +
@PatchMappingPATCH 方法映射,等效于 @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、主要组件

组件名称实现说明
内容协商管理器ContentNegotiationManagerContentNegotiationStrategy 控制策略
媒体类型MediaTypeHTTP 消息媒体类型,如 text/html
消费媒体类型@RequestMapping#consumes请求头 Content-Type 媒体类型映射
生产媒体类型@RequestMapping#produces响应头 Content-Type 媒体类型映射
HTTP消息转换器HttpMessageConverterHTTP 消息转换器,用于反序列化 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值