I have a Person pojo with some fields can't be null in MyGroup class.
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;
}
I only want to validate the pojo when active = true, so I can't validate in the controller level.
I couldn't get the validation working in my service class:
public class PersonService {
@Validated(MyGroup.class)
public PersonObject createPerson(@Valid Person p) { ... }
public PersonObject updatePerson(Person p) { ... }
}
In PersonsController where I invoked the service class
@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);
}
}
I also couldn't get the email validation working properly. Any help would be appreciated.