使用 List<Integer> 作为具有 javax.validation 的字段验证请求
Validate request with List<Integer> as a field with javax.validation
我正在使用 javax.validation
在使用 spring 开发的 API 中验证请求。
我必须验证 List<Integer>
以便所有值都必须是正数并且列表的大小必须完全等于某个固定数字,例如2.
public class RequestPoJo {
private List<Integer> values;
}
有效请求应为 [1,2]
无效请求应为[-1,2]或[1,2,3]
编辑:
刚刚意识到你想如何获得你的有效载荷。在那种情况下,我认为最好是编写自己的约束验证器(注释):
注解:
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@NotNull
@Size(min = 2, max = 2)
@Target( { METHOD, FIELD, ANNOTATION_TYPE, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = {ValidRequestArrayValidator.class})
@Documented
public @interface ValidRequestArray {
String message() default "{com.mycompany.constraints.ValidRequestArray}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
验证者:
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;
public class ValidRequestArrayValidator implements ConstraintValidator<ValidRequestArray, List<Integer>> {
@Override
public boolean isValid(List<Integer> value, ConstraintValidatorContext context) {
return value.stream().noneMatch(n -> n < 0);
}
}
休息控制器:
@RestController
@Validated
public class TestController
@RequestMapping("/test")
void test(
@RequestBody
@ValidRequestArray
List<Integer> values) throws JsonProcessingException {
System.out.println(new ObjectMapper().writeValueAsString(values));
}
}
我正在使用 javax.validation
在使用 spring 开发的 API 中验证请求。
我必须验证 List<Integer>
以便所有值都必须是正数并且列表的大小必须完全等于某个固定数字,例如2.
public class RequestPoJo {
private List<Integer> values;
}
有效请求应为 [1,2]
无效请求应为[-1,2]或[1,2,3]
编辑:
刚刚意识到你想如何获得你的有效载荷。在那种情况下,我认为最好是编写自己的约束验证器(注释):
注解:
import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
@NotNull
@Size(min = 2, max = 2)
@Target( { METHOD, FIELD, ANNOTATION_TYPE, PARAMETER})
@Retention(RUNTIME)
@Constraint(validatedBy = {ValidRequestArrayValidator.class})
@Documented
public @interface ValidRequestArray {
String message() default "{com.mycompany.constraints.ValidRequestArray}";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
验证者:
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.List;
public class ValidRequestArrayValidator implements ConstraintValidator<ValidRequestArray, List<Integer>> {
@Override
public boolean isValid(List<Integer> value, ConstraintValidatorContext context) {
return value.stream().noneMatch(n -> n < 0);
}
}
休息控制器:
@RestController
@Validated
public class TestController
@RequestMapping("/test")
void test(
@RequestBody
@ValidRequestArray
List<Integer> values) throws JsonProcessingException {
System.out.println(new ObjectMapper().writeValueAsString(values));
}
}