Download Jersey 2.1 User Guide

Transcript
Chapter 16. Bean Validation Support
Validation is a process of verifying that some data obeys one or more pre-defined constraints. This
chapter describes support for Bean Validation [http://beanvalidation.org/] in Jersey in terms of the needed
dependencies, configuration, registration and usage. For more detailed description on how JAX-RS
provides native support for validating resource classes based on the Bean Validation refer to the chapter
in the JAX-RS spec [http://jcp.org/en/jsr/detail?id=339].
16.1. Bean Validation Dependencies
Bean Validation support in Jersey is provided as an extension module and needs to be mentioned explicitly
in your pom.xml file (in case of using Maven):
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.1</version>
</dependency>
Note
If you're not using Maven make sure to have also all the transitive dependencies
(see jersey-bean-validation [https://jersey.java.net/project-info/2.1/jersey/project/jersey-beanvalidation/dependencies.html]) on the classpath.
This module depends directly on Hibernate Validator [http://www.hibernate.org/subprojects/
validator.html] which provides a most commonly used implementation of the Bean Validation API spec.
If you want to use a different implementation of the Bean Validation API, use standard Maven mechanisms
to exclude Hibernate Validator from the modules dependencies and add a dependency of your own.
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.1</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
16.2. Enabling Bean Validation in Jersey
As stated in Section 4.1, “Auto-Discoverable Features”, Jersey Bean Validation is one of the modules
where you don't need to explicitly register it's Features (ValidationFeature [http://jersey.java.net/nonav/
apidocs/snapshot/jersey/org/glassfish/jersey/server/validation/ValidationFeature.html]) on the server as
it's features are automatically discovered and registered when you add the jersey-beanvalidation module to your classpath. There are three Jersey specific properties that could disable
automatic discovery and registration of Jersey Bean Validation integration module:
159