1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.创建实体类,以下面举例
@Data
public class AddressBookDto {
//用户id
@NotNull
@Min(2222)
private Long userId;
//收货人 或 雇员名称
@NotNull
@Size(min = 2, max = 30)
private String consignee;
//手机号
private String phone;
//性别 0 女 1 男
private String sex;
//省级区划编号
private String provinceCode;
//省级名称
private String provinceName;
//市级区划编号
private String cityCode;
//市级名称
private String cityName;
//区级区划编号
private String districtCode;
//区级名称
private String districtName;
//详细地址
private String detail;
//标签 比如 公司、家
private String label;
//是否默认 0 否 1是
private Integer isDefault;
//是否删除
private Integer isDeleted;
}
3.在controller代码里面加入BindingResult bindingResult,在要验证的实体中前加@Valid
@PostMapping("/save")
public Result<AddressBook> save(@Parameter(description = "addressBookDto")
@RequestBody @Valid AddressBookDto addressBookDto, BindingResult bindingResult) {
AddressBook addressBook = new AddressBook();
BeanUtils.copyProperties(addressBookDto, addressBook);
return Result.success(addressBookService.save(addressBook));
}
4.在AOP中加入以下代码
@Aspect
@Component
public class WebLogAspect {
@Autowired
ObjectMapper objectMapper;
Logger logger = LoggerFactory.getLogger(WebLogAspect.class);
@Around("execution(* com.integration.scaffold.relationaldataaccess.mysql.controller.*.*(..))")
public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
// 记录调用的方法和参数
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Long startTime = System.currentTimeMillis();
logger.info("请求ip是:{}", request.getRemoteAddr());
logger.info("请求url是: {}", request.getRequestURL().toString());
logger.info("请求方法是: {}", request.getMethod());
logger.info("处理Method:{},请求方法:{}", pjp.getSignature().getDeclaringTypeName(), pjp.getSignature().getName());
logger.info("请求参数是:{}", pjp.getArgs());
for (Object obj : pjp.getArgs()) {
if (obj instanceof BindingResult) {
BindingResult bindingResult = (BindingResult) obj;
if (bindingResult.hasErrors()) {
List<ObjectError> errors = bindingResult.getAllErrors();
StringBuffer sb = new StringBuffer();
for (ObjectError objectError : errors) {
sb.append(Arrays.stream(objectError.getArguments()).findFirst() + "---" +
objectError.getCode() + "---" + objectError.getDefaultMessage());
}
logger.info("返回结果是:{}", Result.fail(sb.toString()));
Long endTime = System.currentTimeMillis();
logger.info("执行方法耗时:{}秒", (endTime - startTime) / 1000.0);
return Result.fail(sb.toString());
}
}
}
Object retVal = pjp.proceed();
// stop stopwatch
logger.info("返回结果是:{}", objectMapper.writeValueAsString(retVal));
Long endTime = System.currentTimeMillis();
logger.info("执行方法耗时:{}秒", (endTime - startTime) / 1000.0);
return retVal;
}
}
启动项目,进行测试
当不符合条件时:
当符合条件时:
点击比如@Min所在的包里面有很多的验证条件,都是可以使用的哦
标签:info,String,验证,private,表单,BindingResult,logger,pjp,输入 From: https://www.cnblogs.com/yunzhiliuandyunchaoliu/p/18190517