首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >ResponseBody、RequestBodyAdvice解读

ResponseBody、RequestBodyAdvice解读

作者头像
关忆北.
发布2021-12-07 16:35:53
发布2021-12-07 16:35:53
4840
举报
文章被收录于专栏:关忆北.关忆北.

BodyAdvice用处:

  • 参数过滤
  • 字符的编码
  • 第三方的加解密

RequestBodyAdvice:

作用:进行统一接口请求参数处理

优势:全局解决方案,使原本在Controller处理的内容统一全局处理,精简代码量、降低耦合度。

官方API解读:

Allows customizing the request before its body is read and converted into an Object and also allows for processing of the resulting Object before it is passed into a controller method as an @RequestBody or an HttpEntity method argument.

Implementations of this contract may be registered directly with the RequestMappingHandlerAdapter or more likely annotated with @ControllerAdvice in which case they are auto-detected.

允许在请求体被读取并转换为对象之前自定义请求,也允许在结果对象作为@RequestBody或HttpEntity方法参数传递给控制器方法之前处理结果对象。 这个契约的实现可以直接注册到RequestMappingHandlerAdapter,或者更有可能用@ControllerAdvice注解,在这种情况下,它们会被自动检测到。 RequestBodyAdvice仅对使用了@RqestBody注解的生效 , 因为它原理上还是AOP , 所以GET方法是不会操作的.

ResponseBodyAdvice

作用:进行统一接口返回数据处理

优势: 需要对返回接口数据进行二次包装处理返回体,如修改返回值、数据加密。

官方API解读:

Allows customizing the response after the execution of an @ResponseBody or a ResponseEntity controller method but before the body is written with an HttpMessageConverter.

Implementations may be registered directly with RequestMappingHandlerAdapter and ExceptionHandlerExceptionResolver or more likely annotated with @ControllerAdvice in which case they will be auto-detected by both.

允许在@ResponseBody或ResponseEntity控制器方法执行之后,但使用HttpMessageConverter编写主体之前自定义响应。 实现可以直接用RequestMappingHandlerAdapter和ExceptionHandlerExceptionResolver注册,或者更可能用@ControllerAdvice注释,在这种情况下,它们都将被自动检测到。

具体使用方式可以参考我的开源项目

eshare

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/01/03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • BodyAdvice用处:
  • RequestBodyAdvice:
  • ResponseBodyAdvice
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档