我的模型中没有几个数字字段。 SpringValidatorAdapter没有验证这些约束。
如果我声明如下字段的数据类型
public class Demo{
@Max(value = "7", message = "{max message}")
@Min(value = "1", message = "{min message}")
private Integer amount1;
@Max(value = "7", message = "{max message}")
@Min(value = "1", message = "{min message}")
private Integer amount2;
//getters and setters
}
控制器代码段
@PostMapping(value = { "/url" }, produces = "application/json;charset=utf-8")
@ResponseBody
public String test(@RequestBody Map<String, String> formInputs) {
Demo dmo=new Demo();
String amount1 = formInputs.get("userinput1");
dmo.setAmount1(Integer.parseInt(amount1));
String amount2 = formInputs.get("userinput2");
dmo.setAmount2(Integer.parseInt(amount2));
final Validator javaxValidator = Validation.buildDefaultValidatorFactory().getValidator();
final SpringValidatorAdapter validator = new SpringValidatorAdapter(javaxValidator);
errors = new BeanPropertyBindingResult(dmo, dmo.getClass().getName());
validator.validate(dmo, errors);
以下我尝试引用的内容
difference between @size(max = value ) and @min(value) @max(value)
Spring - validation of Integer attribute
注意:如果我将其更改为String,则工作正常。