spring-boot may be used @validated check data, if the data exception thrown will be unified, convenient unitary anomaly centers.
For example, we determine the legality of an input parameter, can be used as follows
Use a foundation
Because the spring-boot basic package has been introduced, it can be used directly of
a statement on the first data validation controller needs
@RequestMapping(value="/url.json",method= {RequestMethod.POST})
@ResponseBody
@Transactional
public Result<?> xxmethod( @RequestBody @Validated XoPO xoPo)
throws ParseException, UnsupportedEncodingException {}
2 then the field is declared to be verified on the bean
@data
public class XoPO{
@validated
private List<OrderPerson> personList;
@NotNull
@Size(max=32,message="code is null")
private String code;
@NotBlank
@Size(max=32,message="product is null")
private String product;
}
Then, when the input conditions are not met, the exception is thrown, then unified by the abnormal processing center
can also be used BindingResult, but after using this manual must handle exceptions, invade normal logical process, not recommended
Two common types of annotations
Be careful not to misuse the exception type, such as int is not available on @size
Common annotated as follows
@AssertFalse 校验false
@AssertTrue 校验true
@DecimalMax(value=,inclusive=) 小于等于value,
inclusive=true,是小于等于
@DecimalMin(value=,inclusive=) 与上类似
@Max(value=) 小于等于value
@Min(value=) 大于等于value
@NotNull 检查Null
@Past 检查日期
@Pattern(regex=,flag=) 正则
@Size(min=, max=) 字符串,集合,map限制大小
@Validate 对po实体类进行校验
Three nested parity
If a class contains another entity class, it can be added on top @Validated, such as the above
public class XoPO {
@validated
private List<PersonDetailPO> personList;
}
Four @pathvariable check
spring-boot may not currently support the validation of parameters: HTTPS: //jira.spring.io/browse ...
public Result<?> xoById( @NotNull @NotBlank @Size(min=10,max=32)@PathVariable(value="accountId") String id) {}
But there is not an exception is thrown, possibly resolve in the next version of the spring, or do not @Pathvariable, while in the service
Class XoService{
public xoMethon( @NotNull String id){
}
}
To solve
[Reserved] https://segmentfault.com/a/1190000011712893