Spring对对象属性的校验
来源:互联网 发布:js正则匹配字符串 编辑:程序博客网 时间:2024/05/21 21:41
场景:对对象的参数进行校验,如果不通过抛出异常
1.编写需要被校验的对象
public class Pet { @NotEmpty(message = "不能为空") private String name; @Range(min = 0, max = 150, message = "范围在0—150之间") private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Pet{" + "name='" + name + '\'' + ", age=" + age + '}'; }}
2.定义异常体系
public class BaseException extends RuntimeException { private int code; BaseException(ExceptionDesception e){ super(e.getMsg()); this.code=e.getCode(); } BaseException(int code,String msg){ super(msg); this.code=code; } public int getCode() { return code; } public void setCode(int code) { this.code = code; }}
2.1 定义参数异常类
public class ParamInvalidException extends BaseException { public ParamInvalidException(ExceptionDesception e) { super(e); } public ParamInvalidException(int code, String msg) { super(code, msg); }}2.2定义异常描述类
public enum ExceptionDesception { NO_PERMISSION(-1,"无权限"), PARAM_INVALID(-2,"参数错误"); private int code; private String msg; ExceptionDesception(int code,String msg){ this.code=code; this.msg=msg; } public int getCode() { return code; } public String getMsg() { return msg; }}2.3定义异常处理
@ControllerAdvicepublic class BaseExceptionHandler { @ExceptionHandler({WebApiException.class}) @ResponseBody public Object expHandler(WebApiException e) { return createResult(e); } public JSONObject createResult(BaseException e) { return createResult(e.getCode(), e.getMessage()); } public JSONObject createResult(int code, String msg) { JSONObject result = new JSONObject(); result.put("code", code); result.put("msg", msg); return result; } @ExceptionHandler({ParamInvalidException.class}) @ResponseBody public Object expValidHandler(ParamInvalidException e) { return createResult(e); }}
3.定义校验规则
public class ValidationUtils { private static final Validator VALIDATOR; static { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); VALIDATOR = factory.getValidator(); } private ValidationUtils() { } public static <T> void validate(T entity, Class<?>... groups) { Set<ConstraintViolation<T>> constraintViolations = VALIDATOR.validate(entity, groups); if (constraintViolations.isEmpty()) { return; } ConstraintViolation<T> first = constraintViolations.iterator().next(); throw new ParamInvalidException(ExceptionDesception.PARAM_INVALID.getCode(),first.getMessage()); }}4.编写测试controller
@Controller@RequestMapping("h1")public class TestController { @RequestMapping(value = "h2") public @ResponseBody Object testHeaderContext(@RequestBody Pet pet) { ValidationUtils.validate(pet); return pet.getName(); }}
阅读全文
0 0
- Spring对对象属性的校验
- Spring对对象的维护
- struts对对象属性的监听
- 对对象的某些属性进行排序
- java List对对象的属性排序
- unity编辑器对对象属性的编辑
- 对对象的理解
- KVC中对对象的属性赋值的顺序
- 对对象按对象的某个属性进行排序
- apache或hibernate对对象属性的合并
- 根据List里面的对象属性对对象排序
- STL对对象的排序
- Hibernate对对象的管理
- JavaScript的prototype对对象
- Redis对对象的操作
- 对对象的简单操作
- C#读取对象实例的值和对对象的属性自动赋值方法
- vue的观察者,不会对对象的属性进行冒泡监听
- 浅谈IT风投
- 67. Add Binary
- 结构化机器学习项目小结
- WCF服务编程(4)——以编程的方式配置终结点
- 每天一个linux命令(13):less 命令
- Spring对对象属性的校验
- mysql修改连接密码
- ES6 基础
- python总结4
- 关于比特币钱包
- [Codeforces Round #452 (Div. 2)] Splitting in Teams
- Deploy Django in Apache
- linux I2C 驱动之----i2c驱动的注册过程(i2c_register_driver->driver_register(&driver->driver)->driver_find)
- 《程序员的自我修养》读书笔记