默認(rèn)對(duì)全局請(qǐng)求參數(shù)進(jìn)行驗(yàn)證聪蘸,無(wú)需加注解 @Validated
定制全局異常處理器GlobalExceptionHandler
/**
* 全局異常處理
*
* @author seer
* @version 1.0
* @date 2022/6/9 15:40
*/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* 請(qǐng)求方式不支持
*/
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public Result handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, HttpServletRequest request) {
log.warn("請(qǐng)求方式不支持 {} {}", request.getServletPath(), e.getMethod());
return Result.fail(e.getMessage());
}
/**
* 參數(shù)驗(yàn)證錯(cuò)誤
*/
@ExceptionHandler(BindException.class)
public Result handleBindException(BindException e, HttpServletRequest request) {
String message = e.getAllErrors().get(0).getDefaultMessage();
log.warn("請(qǐng)求參數(shù)錯(cuò)誤 {} {}", request.getServletPath(), message);
return Result.fail(Result.Type.REQUEST_PARAM_ERROR, message);
}
/**
* 參數(shù)綁定異常
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
public Result handleBindException(MissingServletRequestParameterException e, HttpServletRequest request) {
String message = e.getMessage();
log.warn("參數(shù)綁定異常 {} {}", request.getServletPath(), message);
return Result.fail(Result.Type.REQUEST_PARAM_ERROR, e.getParameterName() + " 未提供");
}
/**
* 參數(shù)驗(yàn)證錯(cuò)誤
*/
@ExceptionHandler(ConstraintViolationException.class)
public Result handleBindException(ConstraintViolationException e, HttpServletRequest request) {
String message = e.getMessage();
log.warn("參數(shù)驗(yàn)證錯(cuò)誤 {} {}", request.getServletPath(), message);
return Result.fail(Result.Type.REQUEST_PARAM_ERROR, e.getMessage());
}
}
自定義AOP GlobalValidAspect
- 拋出
ConstraintViolationException
異常
package com.sdhs.wash.admin.component;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.beanvalidation.SpringValidatorAdapter;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;
/**
* 請(qǐng)求參數(shù)驗(yàn)證
* 基本數(shù)據(jù)類型犹撒,由@RequestParam注解,走框架
* 對(duì)象由此校驗(yàn),不區(qū)分是否添加 @RequestBody
*
* @author seer
* @date 2022-06-28 22:06:04
*/
@Aspect
@Component
@Slf4j
public class GlobalValidAspect {
@Autowired
private SpringValidatorAdapter validator;
/**
* 前置
*
* @param joinPoint joinPoint
*/
@Before("execution(public * com.sdhs.oilcoreserver.admin.controller.*.*(..))")
public void atBefore(JoinPoint joinPoint) {
for (Object arg : joinPoint.getArgs()) {
Set<ConstraintViolation<Object>> violationSet = validator.validate(arg);
if (!violationSet.isEmpty()) {
throw new ConstraintViolationException(violationSet.iterator().next().getMessage(), null);
}
return;
}
}
}
controller
中表現(xiàn)
被@RequestBody
注解的json請(qǐng)求參數(shù)
/**
* 修改密碼
*
* @param requestDTO requestDTO
* @return object
*/
@ApiOperation(value = "修改密碼")
@PostMapping("/change-pwd")
public Result changePwd(@RequestBody UserChangePwdRequestDTO requestDTO) {
log.info("修改密碼袍暴,請(qǐng)求 {}", requestDTO);
Result response = service.changePwd(requestDTO);
log.info("修改密碼倒源,響應(yīng) {}", response.toSensitiveString());
return response;
}
封裝為實(shí)體的form請(qǐng)求參數(shù)
@ApiOperation(value = "用戶查詢",
response = UserQryResponseDTO.class)
@GetMapping("/user/query")
public Result userQuery(KeywordRequestDTO requestDTO) {
Result response = service.userQuery(requestDTO);
if (log.isDebugEnabled()) {
log.debug("用戶查詢,響應(yīng) {}", response);
}
return response;
}
被 @RequestParam
注解的參數(shù)
- 此類參數(shù)孵稽,由Spring框架校驗(yàn)许起,校驗(yàn)失敗拋出
BindException
異常
/**
* 字典查詢
*
* @return object
*/
@ApiOperation(value = "字典查詢", response = DictQryResponseDTO.class)
@ApiImplicitParam(name = "group", value = "字典分組")
@GetMapping("/dict-query")
public Result dictQuery(@RequestParam(value = "group") String group) {
return service.dictQuery(group);
}