3.在controller的入参处添加@Validated注解(@Validated可以指定groups区别验证,@Valid不可以指定groups @Validated(addGroup.class)就是给参数进行了分组校验 定义分组接口: public interface IGroupA { } public interface IGroupB { } public class StudentBean implements Serializable{ @NotBlank(message = "用户名不...
class}) private Integer id; @NotBlank(message = "名字不能为空", groups = {UpdateGroup.class, AddGroup.class}) @Size(min = 6, max = 12, message = "名字的长度在6到12之间", groups = {UpdateGroup.class, AddGroup.class}) private String username; @NotNull(message = "年龄不能为空",...
@Null(message = "更新必须不为空", groups = {UpdateGroup.class}) private String id; @NotNull(message = "名称不能为空", groups = {AddGroup.class, UpdateGroup.class}) private String userName; @NotNull(message = "年龄不能为空",groups = {AddGroup.class, UpdateGroup.class}) private Integ...
public interface AddGroup {} public interface UpdateGroup {} // @Validated注解value方法指定分组UpdateGroup.class时校验 @NotBlank(message = "用户ID必须提交!", groups = UpdateGroup.class) private String id; // @Validated注解value方法指定分组AddGroup.class或者分组UpdateGroup.class时校验 @NotBlank(me...
@Validated(value={UserDto.Add.class}) @Validated(value={UserDto.Update.class}) 进行了分组校验 UserDto 对象代码 package com.olive.vo; import javax.validation.constraints.NotNull; import javax.validation.constraints.Null; public class UserDto extends BaseVo{ ...
public class FirstController { @RequestMapping("/addPeople") //不需验证ID public @ResponseBody String addPeople(@Validated People pYfiCbehGrV,BindingResult result) { System.out.println("people's ID:" + p.getId()); if(result.hasErrors()) ...
public class ValidationUtils{ public static <E, T extends ValidationBaseDTO> List<T>validate(Validator validator, E e) { return validate(validator, e, Default.class); } public static <E, T extends ValidationBaseDTO> List<T> validate(Validator validator, E e, Class<?> groupClass) { ...
public ResultaddGroup(@Validated(value = Group.class)@RequestBodyUserGroupSequence user) {returnResult.buildSuccess(user); } } AI代码助手复制代码 使用@GroupSequence注解指定分组校验顺序后,第一组分组的约束的校验没有通过后,就不会进行第二组分组的约束的校验 ...
// group是可以⾃定义的,我默认定义了Add,Update,Delete,Search这四个组 @PostMapping("update")public boolean updateUser(@Validated({Update.class, Default.class}) @RequestBody User user) { ProdAllotStandard info = allotStandardService.getProdAllotStandardWithDetailById(id);return info;} // 当然...
importcom.valid.interfaces.Group;importcom.valid.pojo.People;importcom.valid.pojo.Person;@ControllerpublicclassFirstController{@RequestMapping("/addPeople")//不需验证IDpublic@ResponseBodyStringaddPeople(@Validated({Group.class})People p,BindingResult result){if(result.hasErrors()){return"0";}return"1...