1. 之前写过一篇关于如何实现web端优雅校验的文章(https://www.jianshu.com/p/23e5768a34cd)今天接着补充一下如何实现service端的校验,与君共勉.
2. 源代码
maven依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
实体对象:
package com.example.demo.model;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.Range;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class NewStudent {
@Range(min = 15, max = 65, message = "年龄必须在15岁到65岁")
private int age;
@Size(min = 2, max = 30)
private String name;
@NotEmpty(message = "自定义错误信息,score不能为空!")
private String score;
@Future(message = "生效时间必须大于当前时间")
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private LocalDateTime createTime;
}
interface:
package com.example.demo.service;
import com.example.demo.model.NewStudent;
import javax.validation.Valid;
/**
* @Author: Robin
* @Date: 2020/4/11 7:01 PM
* @aphorism: "Stay hungry, Stay foolish"
*/
public interface Test1Service {
/**
* insertStudent
* @param student
* @return
*/
int insertStudent(@Valid NewStudent student);
}
service实现类:
package com.example.demo.service;
import com.example.demo.model.NewStudent;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
/**
* @Author: Robin
* @Date: 2020/4/11 7:03 PM
* @aphorism: "Stay hungry, Stay foolish"
*/
@Service
@Validated
public class Test01ImplService implements Test1Service {
@Override
public int insertStudent(NewStudent student) {
System.out.println("niu");
return 0;
}
}
AOP全局异常拦截器(非web)
package com.example.demo.base;
import com.example.demo.model.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import javax.validation.ConstraintViolationException;
/**
* @Author: Robin
* @Date: 2020/4/11 7:49 PM
* @aphorism: "Stay hungry, Stay foolish"
*/
@Component
@Aspect
public class ExceptionHandler {
@Pointcut("execution(* com.example.demo.controller.*.*(..))")
public void constraintViolationException() {
}
@Around("constraintViolationException()")
public Object catchException(ProceedingJoinPoint point) throws Throwable {
try {
System.out.println("david");
return point.proceed();
} catch (ConstraintViolationException e) {
System.out.println(e.getConstraintViolations());
return new Result(101, e.getMessage(), "funny");
} catch (Exception e) {
System.out.println("muddy");
return new Result(111, e.getMessage(), null);
}
}
}
准备模拟测试数据
package com.example.demo.controller;
import com.example.demo.model.NewStudent;
import com.example.demo.model.Result;
import com.example.demo.service.Test1Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.time.LocalDateTime;
/**
* @Author: Robin
* @Date: 2020/4/11 8:07 PM
* @aphorism: "Stay hungry, Stay foolish"
*/
@RestController
public class ExceptionTest {
@Resource
private Test1Service test1Service;
@GetMapping(value = "/test002")
public Result testException() {
NewStudent ss = new NewStudent();
ss.setAge(25);
ss.setScore("100");
ss.setName("robin");
LocalDateTime t =LocalDateTime.now().minusHours(1);
ss.setCreateTime(t);
test1Service.insertStudent(ss);
return new Result(200, "", ss);
}
}
测试效果: