如何通过RestControllerAdvice优化返回体的内容?

2026-05-27 21:481阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计209个文字,预计阅读时间需要1分钟。

如何通过RestControllerAdvice优化返回体的内容?

使用`@RestControllerAdvice`注解,创建自定义类以处理响应:

java/** * desc: 自定义响应体处理 * author: cjq * date: 2022/10/11 */@RestControllerAdvice(value={com.xxx.sjcj}, annotations={ResultWrapper.class})public class CustomResponseBodyAdvice implements ResponseBodyAdvice {

如何通过RestControllerAdvice优化返回体的内容?

使用注解@RestControllerAdvice

新建自定义类:

/** * desc * * @author cjq * @date 2022/10/11 */ @RestControllerAdvice(value={"com.xxx.sjcj"},annotations = {ResultWrapper.class}) public class CustomResponseBodyAdvice implements ResponseBodyAdvice<Object> { @Override public boolean supports(MethodParameter returnType, Class converterType) { return true; } @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { SjcjResponse res = new SjcjResponse(); JSONObject bodyObj = JSONObject.parseObject(JSON.toJSONString(body)); int code = bodyObj.getInteger("code"); if(HttpStatus.SUCCESS!=code){ res.setCode(RespEnum.UNKNOW_ERROR.getCod()); res.setDes(bodyObj.getString("msg")); } return res.toString(); } }

在需要重构返回体的方法上加注解ResultWrapper

ResultWrapper类:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ResultWrapper { }

使用在方法中:

@PostMapping @ResultWrapper public AjaxResult addBatch(@RequestBody List<SjcjSyfwEntity> list){ //... 省略 }

本文共计209个文字,预计阅读时间需要1分钟。

如何通过RestControllerAdvice优化返回体的内容?

使用`@RestControllerAdvice`注解,创建自定义类以处理响应:

java/** * desc: 自定义响应体处理 * author: cjq * date: 2022/10/11 */@RestControllerAdvice(value={com.xxx.sjcj}, annotations={ResultWrapper.class})public class CustomResponseBodyAdvice implements ResponseBodyAdvice {

如何通过RestControllerAdvice优化返回体的内容?

使用注解@RestControllerAdvice

新建自定义类:

/** * desc * * @author cjq * @date 2022/10/11 */ @RestControllerAdvice(value={"com.xxx.sjcj"},annotations = {ResultWrapper.class}) public class CustomResponseBodyAdvice implements ResponseBodyAdvice<Object> { @Override public boolean supports(MethodParameter returnType, Class converterType) { return true; } @Override public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { SjcjResponse res = new SjcjResponse(); JSONObject bodyObj = JSONObject.parseObject(JSON.toJSONString(body)); int code = bodyObj.getInteger("code"); if(HttpStatus.SUCCESS!=code){ res.setCode(RespEnum.UNKNOW_ERROR.getCod()); res.setDes(bodyObj.getString("msg")); } return res.toString(); } }

在需要重构返回体的方法上加注解ResultWrapper

ResultWrapper类:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ResultWrapper { }

使用在方法中:

@PostMapping @ResultWrapper public AjaxResult addBatch(@RequestBody List<SjcjSyfwEntity> list){ //... 省略 }