Java 如何在Spring Boot中验证服务类中的有效负载

Java 如何在Spring Boot中验证服务类中的有效负载,java,spring-boot,validation,Java,Spring Boot,Validation,我有一个Person pojo,其中某些字段在MyGroup类中不能为null public class Person { @NotNull(message = "address not null", groups = { MyGroup.class }) private String address; @NotNull(message = "groupId not null", groups = { MyGroup.class }) private

我有一个Person pojo,其中某些字段在MyGroup类中不能为null

 public class Person {
     @NotNull(message = "address not null", groups = { MyGroup.class })
     private String address;

     @NotNull(message = "groupId not null", groups = { MyGroup.class })
     private UUID groupId;

     private String name;
     private String phoneNumber;

     @Email
     private String email;
     private Boolean active;
}
我只想在active=true时验证pojo,因此无法在控制器级别进行验证

我无法在我的服务类中进行验证:

 public class PersonService {
      @Validated(MyGroup.class)
      public PersonObject createPerson(@Valid Person p) { ... }

      public PersonObject updatePerson(Person p) { ... }
 }
在PersonsController中,我调用了服务类

@Autowired
private PersonService personService;

@PostMapping
public PersonObject createPerson(@RequestBody Person payload) {
   if (payload.getActive()) {
       // want to validate the Not null fields
       return personService.createPerson(payload);
   } else {
       // does not need to validate fields 
       return personService.updatePerson(payload);
   }
}

我也无法使电子邮件验证正常工作。任何帮助都将不胜感激

你是如何调用你的服务类的?@chrylis cautiouslyoptimistic来自调用服务方法代码的控制器,请包括你是如何获得
PersonClass
@chrylis cautiouslyoptimistic-update的实例的。我想你应该编写自己的验证类,让Spring为你做这项工作。在对象到达控制器之前,应该对其进行完全验证。