I wrote example of endpoints to understand the meaning of my question.
I realised that my tests didnt pass due to the @Valid
annotation.
Eg. I got two endpoints if someone is hitting the old one there is a need to check if Email is set becuase it has a @NotBlank
annotation.
But when I'm asking my endpoint with a set email the @Valid
is not executed I realised that after doing tests with invalid data. And I cannot set the @Valid
in the oldPutContact
because there are some contacts without email.
My question is why putContact
didn't execute the @Valid
annotation.
@PutMapping(value = "/contact/{contactid}", )
public String oldPutContact(@RequestBody Contact contact)
if(StringUtils.isBlank(contact.getEmail())){
contact.setEmail("randomEmail@ups.cm") ;
}
return putContact(contact);
}
@PutMapping(value = "/contact/update/{contactid}")
public String putContact(@RequestBody @Valid Contact contact) {
contactService.updateContact(contact);
}
Imagine Contact Entity looks like this
@Builder(toBuilder=true)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Contact {
private static final String SOME_PATTERN = "";
private static final String SOME_EMAIL_PATTERN = "";
private static final String SOME_PATTERN2 = "";
@NotBlank
private String id;
@NotNull
private Boolean isActive;
@NotBlank
@Pattern(regexp = SOME_PATTERN)
private String name;
@NotBlank
@Pattern(regexp = SOME_PATTERN)
private String surname;
@NotBlank
@Email(regexp = SOME_EMAIL_PATTERN)
private String email;
@NotBlank
@Pattern(regexp = SOME_PATTERN2)
private String telephone;
}