1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| package cn.exrick.xboot.core.common.exception;
import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.security.access.AccessDeniedException; import org.springframework.validation.BindException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.validation.ConstraintViolationException;
@Slf4j @RestControllerAdvice public class ExceptionHandler {
@ExceptionHandler(BindException.class) @ResponseStatus(value = HttpStatus.OK) public Result<Object> handleBindException(BindException e) {
StringBuilder sb = new StringBuilder(); e.getFieldErrors().forEach(error -> { String fieldName = error.getField(); String message = error.getDefaultMessage(); sb.append(fieldName + "-" + message + ";"); }); String result = sb.toString(); if (result.length() > 0) { result = result.substring(0, result.length() - 1); } return new ResultUtil<>().setErrorMsg(500, result); }
@ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(value = HttpStatus.OK) public Result<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
StringBuilder sb = new StringBuilder(); e.getBindingResult().getFieldErrors().forEach(error -> { String fieldName = error.getField(); String message = error.getDefaultMessage(); sb.append(fieldName + "-" + message + ";"); }); String result = sb.toString(); if (result.length() > 0) { result = result.substring(0, result.length() - 1); } return new ResultUtil<>().setErrorMsg(500, result); }
@ExceptionHandler(ConstraintViolationException.class) @ResponseStatus(value = HttpStatus.OK) public Result<Object> handleConstraintViolationException(ConstraintViolationException e) {
StringBuilder sb = new StringBuilder(); e.getConstraintViolations().forEach(error -> { String fieldName = error.getPropertyPath().toString(); String message = error.getMessageTemplate(); sb.append(fieldName + "-" + message + ";"); }); String result = sb.toString(); if (result.length() > 0) { result = result.substring(0, result.length() - 1); } return new ResultUtil<>().setErrorMsg(500, result); }
@ExceptionHandler(CaptchaException.class) @ResponseStatus(value = HttpStatus.OK) public Result<Object> handleCaptchaException(CaptchaException e) {
String errorMsg = "CaptchaException exception"; if (e != null) { errorMsg = e.getMsg(); log.warn(e.getMsg(), e); } return new ResultUtil<>().setErrorMsg(500, errorMsg); }
@ExceptionHandler(AccessDeniedException.class) @ResponseStatus(value = HttpStatus.OK) public Result<Object> handleAccessDeniedException(AccessDeniedException e) {
String errorMsg = "AccessDeniedException exception"; if (e != null) { errorMsg = e.getMessage(); log.warn(e.getMessage(), e); } return new ResultUtil<>().setErrorMsg(500, errorMsg); }
@ExceptionHandler(Exception.class) @ResponseStatus(value = HttpStatus.OK) public Result<Object> handleException(Exception e) {
String errorMsg = "Exception"; if (e != null) { errorMsg = e.getMessage(); log.error(e.toString(), e); } return new ResultUtil<>().setErrorMsg(500, errorMsg); } }
|
上面的MethodArgumentNotValidException会拦截下面参数验证的异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import io.swagger.annotations.ApiModelProperty; import lombok.Data;
import javax.validation.constraints.NotNull; import java.io.Serializable; import java.util.List;
@Data public class SearchLineFlowVo implements Serializable {
@ApiModelProperty(value = "综合分析(类别:断面 线路 车站)||换成客流查询(类别: 换入 换出)") private String categoryType;
@ApiModelProperty(value = "断面名称 或 线路名称") @NotNull(message = "断面名称或线路名称不能为空") private List<String> categoryNames;
@ApiModelProperty(value = "日期到日") @NotBlank(message = "日期不能为空") private String dateOfDay;
@ApiModelProperty(value = "颗粒度 5 15 30 60 1440") @NotNull(message = "颗粒度不能为空") private String timeSpan;
@ApiModelProperty(value = "页面大小") @NotNull(message = "页面大小不能为空") private PageVo pageVo;
@ApiModelProperty(value = "搜索范围") @NotNull(message = "搜索范围不能为空") private SearchVo searchVo;
@ApiModelProperty(value = "换入 :lineoutdir 换出 :lineindir 括弧:就是反着的:)") private String lineInOrOut;
@ApiModelProperty(value = "对比:yyyy-MM-dd(日) yyyy-MM-dd_yyyy-MM-dd(周or自选) ") private String comparisonDate; @ApiModelProperty(value = "参照:yyyy-MM-dd yyyy-MM-dd(日) yyyy-MM-dd_yyyy-MM-dd(周or自选) ") private String referenceDate;
@ApiModelProperty(value = "周月年") private String dateRange;
}
|