【记录】spring boot 全局捕获异常@ExceptionHandler与@Validated @RequestBody 配合使用
@ExceptionHandler与@Validated @RequestBody 三者配合使用可以很好的做到入参校验,具体demo如下:
接口
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import com.xx.common.core.CommonResult;
import com.xx.product.input.dto.ProductDetailCollectParam; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; @Api(tags = "商品详情页接口")
@RequestMapping(value = "/product")
public interface ProductDetailService { @PostMapping("/collect")
@ApiOperation(value = "收藏商品接口")
public CommonResult collectProduct(@RequestBody @Validated ProductDetailCollectParam productDetailCollectParam); }
入参类
import javax.validation.constraints.NotBlank; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @Data
@ApiModel(value = "商品详情页入参")
public class ProductDetailCollectParam { @NotBlank(message = "用户openid不能为空")
@ApiModelProperty(value = "用户openid")
private String openid; @NotBlank(message = "商品id不能为空")
@ApiModelProperty(value = "商品id")
private String id;
}
全局捕获异常类
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
import javax.validation.ConstraintViolationException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import com.xx.common.core.CommonResult; import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j; @Slf4j
@RestControllerAdvice
public class GlobalExecptionHandler { /**
* 全局异常拦截
* @param e
* @return
*/
@ExceptionHandler(Exception.class)
public CommonResult handler(Exception exception) { StringBuilder errMsg = new StringBuilder();
// 方法参数无效 异常
if(exception instanceof MethodArgumentNotValidException) {
BindingResult bindResult = ((MethodArgumentNotValidException) exception).getBindingResult();
List<FieldError> fieldErrorList = bindResult.getFieldErrors();
fieldErrorList.forEach(fieldErrors -> {
FieldError fieldError = fieldErrors;
if (StrUtil.isNotBlank(errMsg.toString())) {
errMsg.append(",");
}
errMsg.append(fieldError.getDefaultMessage());
}
); }else if (exception instanceof ConstraintViolationException) {
// 约束冲突异常 }
// log.error("异常:",exception);
return CommonResult.failed(errMsg.toString());
}
参考链接:https://www.cnblogs.com/cjyboy/p/11465876.html
参考链接:https://blog.csdn.net/lzhcoder/article/details/98870852
【记录】spring boot 全局捕获异常@ExceptionHandler与@Validated @RequestBody 配合使用的更多相关文章
- Spring Boot 全局异常配置
Spring Boot 全局异常配置,处理异常控制器需要和发生异常的方法在一个类中.使用 ControllerAdvice 注解 package com.li.controller; import o ...
- Spring Boot全局异常处理
本文为大家分享了Spring Boot全局异常处理,供大家参考,具体内容如下 1.后台处理异常 a.引入thymeleaf依赖 <!-- thymeleaf模板插件 --> <dep ...
- 记录Spring Boot大坑一个,在bean中如果有@Test单元测试,不会注入成功
记录Spring Boot大坑一个,在bean中如果有@Test单元测试,不会注入成功 记录Spring Boot大坑一个,在bean中如果有@Test单元测试,不会注入成功 记录Spring Boo ...
- Spring Boot全局支持CORS(跨源请求)
import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet. ...
- spring boot全局配置文件优先级
前两篇介绍的application配置文件,即为spring boot全局配置文件.那么spring boot加载配置文件的时候,怎么确定加载哪个目录下哪个文件呢? spring boot默认的配置文 ...
- spring boot-11.全局捕获异常
1.在Spring boot 中如果发生错误,浏览器访问会默认跳转到Whitelabel Error Page 这个错误页面,如果是客户端访问的话返回JSON格式的错误数据,说明spring boot ...
- Spring Boot 全局异常处理
Spring Boot版本 1.5 @ControllerAdvice public class GlobalExceptionHandler extends ResponseEntityExcept ...
- 使用 JSONDoc 记录 Spring Boot RESTful API
这个博文可以分为两部分:第一部分我将编写一个Spring Boot RESTful API,第二部分将介绍如何使用JSONDoc来记录创建的API.做这两个部分最多需要15分钟,因为使用Spring ...
- Spring Boot 全局排除 spring-boot-starter-logging 依赖
https://blog.csdn.net/u013314786/article/details/90412733 项目里使用了log4j2做日志处理,要排除掉Spring Boot 很多jar里边默 ...
随机推荐
- web classpath路径说明
http://blog.csdn.net/wyswlp/article/details/8886385 http://www.cnblogs.com/Ant-soldier/p/5474085.htm ...
- equals深入理解
package cn.galc.test; public class TestEquals { public static void main(String[] args) { /** * 这里使用构 ...
- Hadoop 服务SYS CPU过高导致宕机问题
最近某hadoop集群多次出现机器宕机,现象为瞬间机器的sys cpu增长至100%,机器无法登录.只能硬件重启,ganglia cpu信息如下: 首先怀疑有用户启动了比较奇葩的job,导致不合理的系 ...
- Linux shell -查找字符(find,xargs,grep)
在当前目录下查找含有jmxremote字符的文件 test@>find . -type f|xargs grep "jmxremote" . 当前目录 -type 查找文件类 ...
- 自定义控件 - 流式布局:TagFlowLayout
在项目中需要用到流式布局的样式,此文学习鸿洋大神的FlowLayout控件,学习使用一下.出自 http://blog.csdn.net/lmj623565791/article/details/38 ...
- C++ STL 二分查找
转载自 https://www.cnblogs.com/Tang-tangt/p/9291018.html 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound ...
- [Forward]C++ Properties - a Library Solution
orig url: https://accu.org/index.php/journals/255 roperties are a feature of a number of programming ...
- eclipse 背景绿豆沙颜色
General -> Editors -> Text Editors -> Appearance color options -> Background color 色调:85 ...
- MVC 源码系列之控制器激活(一)
Controller的激活 上篇说到Route的使用,GetRoute的方法里面获得RouteData.然后通过一些判断,将最后的RouteData中的RouteHandler添加到context.R ...
- idea的类头注释和方法注释的编辑
一:配置类头注释 1:点击file,点击settings 2:点击editor,选择 file and code template ,然后看右侧部分点击include,之后选中File Header ...