Validation
JAXRS 2.1 now enables declarative validation support by leveraging its integration with the Bean Validation API. This is done by using the constraint annotations for validating Beans, method parameters, and return values. These annotations can be placed on resource classes, fields and properties. For example, the following is a sample showing a User
entity having a field level constraint of @NotNull
. The same User
class is then used as an argument to the resource method add
which uses the @Valid
annotation. A POST
request would trigger the validation of the User
entity field name
to meet the @NotNull
criteria:
class User { @NotNull private String name; ... } @Path("/") class ResourceClass { @POST public void add(@Valid User newUser) { ... } }
Similar to the method parameter, the response can also be validated by applying constraints on the return type, as shown:
@GET @Path("{id}") @Valid public User get(@PathParam("id") String id) { ... }
There are features such...