When should there be @ValidateOnExecution used with jersey?

I couldn't understand the gist of the @ValidateOnExecution annotation . Can anyone explain this use case for it?

According to jersey documentation, resource method constraints are automatically checked. This code snippet is from a jersey example .

@GET
@NotNull
@HasId
public List<ContactCard> getContacts() {
    return StorageService.findByName("");
}

@GET
@Path("{id}")
@NotNull(message = "{contact.does.not.exist}")
@HasId
public ContactCard getContact(
        @DecimalMin(value = "0", message = "{contact.wrong.id}")
        @PathParam("id") final Long id) {
    return StorageService.get(id);
}

      

If the constraints are in the pojo, you can initiate validation with @Valid ( See ).

@Path("/")
class MyResourceClass {

@POST
@Consumes("application/xml")
public void registerUser(@Valid User user) {
    ...
}
}

      

So what is @ValidateOnExecution using, other than explicitly disabling validation?

+3


source to share


1 answer


According to the latest documentation from Jersey, the @ValidateOnExecution

annotation should be used for the following purposes:

According to the Bean Validation specification, validation is enabled by default only for so-called restricted methods. The Getter methods defined in the Java Beans specification are not restricted methods, so they will not be checked by default. The @ValidateOnExecution special annotation can be used to selectively enable and disable validation. For example, you can enable validation with the getEmail method shown in the example

@Path("/")
class MyResourceClass {

    @Email
    @ValidateOnExecution
    public String getEmail() {
        return email;
    }
    ...
}

      



The default value for the type attribute for @ValidateOnExecution is IMPLICIT, which causes the getEmail method to be checked.

So it @ValidateOnExecution

can also be used to at least enable validation for getter methods.

+1


source







All Articles