hibernate validate 中文文档 英文文档

所需积分/C币:20 2017-12-07 19:48:25 5.33MB PDF

hibernate validate 中文文档 英文文档 hibernate validate 中文文档 英文文档 hibernate validate 中文文档 英文文档
9.3. MethodDescriptor and ConstructorDescriptor .....114 9.4.ElementDescriptor 116 9.5. GroupConversion Descriptor 119 9.6. ConstraintDescriptor...,... 119 10. Integrating with other frameworks ,,,,.121 10.1. ORM integration .121 10.2. JsF& seam ,.123 10.3.CD ..124 10, 4. Java ee 132 10.5, JavaFX 132 11. Hibernate Validator Specifics .,,.133 11.1. Public apl 133 11.2 Fail fast mode 135 11.3. Relaxation of requirements for method validation in class hierarchies 136 11.4. Programmatic constraint definition and declaration.............. 137 11.5. Applying programmatic constraint declarations to the default validator factory 141 11.6. Advanced constraint composition features ..............141 11.7. Extensions of the path apl 143 11.8. Dynamic payload as part of constraintviolation ..144 11.9. ParameterMessageInterpolator 146 11.10. Resource Bundlelocator ...146 11.11. Custom contexts ....147 11.12. ParaNamer based parametername Provider .149 11.13 Unwrapping values .149 11.14 Providing constraint definitions 153 11.15. Customizing class-loading.......,.... .154 1116. Time providers for@ Future and@Past..…,,,,……155 12. Annotation processor 157 12.1. Prerequisites ..157 12.2. Features .,.,157 12.3. Options........,.. 158 12.4. Using the Annotation Processor 158 12.5. Known issues 163 13. Further reading 164 Preface Validating data is a common task that occurs throughout all application layers, from the presentation to the persistence layer. Often the same validation logic is implemented in each layer which is time consuming and error-prone. To avoid duplication of these validations, developers often bundle validation logic directly into the domain model, cluttering domain classes with validation code which is really metadata about the class itself ava Presentation Business Data access Client Layer Layer Layer Custom validation Custom validation Custom validation Custom validation Custom validation JSR 349- Bean Validation 1. 1-defines a metadata model and API for entity and method validation. The default metadata source are annotations, with the ability to override and extend the meta-data through the use of XML. The aPI is not tied to a specific application tier nor programming model. It is specifically not tied to either web or persistence tier, and is available for both server-side application programming, as well as rich client Swing application developers Java Presentation Business Data Access Client Layer Layer Layer Domain Model Hibernate validator is the reference implementation of this JSR 349. The implementation itself as well as the Bean Validation API and TCK are all provided and distributed under the apache Software license 2.0 Chapter 1. Getting started This chapter will show you how to get started with Hibernate validator, the reference implementation(RI)of Bean Validation For the following quick-start you need .AJDK>=6 · Apache maven An Internet connection (Maven has to download all required libraries) 1.1. Project set up In order to use Hibernate validator within a Maven project, simply add the following dependency to your pom.xm/ Example 1. Hibernate validator Maven dependency <dependency> <groupId>org. hibernates/groupId> <artifactIdshibernate-validator</artifactid> <version>5.4.1. Final</version> </dependency> This transitively pulls in the dependency to the Bean Validation API (javax. validation: validation-api: 1.1.0.Final). 1.1.1. Unified eL Hibernate Validator requires an implementation of the Unified Expression Language (JSR 341) for evaluating dynamic expressions in constraint violation messages (see Default message interpolation). When your application runs in a Java ee container such as JBoss As, an EL implementation is already provided by the container In a Java SE environment, however, you have to add an implementation as dependency to your PoM file. For instance you can add the following two dependencies to use the jsr 341 reference implementation Example 2. Maven dependencies for Unified EL reference implementation <dependency> <groupIdsorg glassfish</groupId> sartifactId>javax. el</artifactId> <version>3.0. 1-b08</version> </dependency> 3 For environments where one cannot provide a eL implementation Hibernate Validator is offering a ParameterMessageInterpolator. However, the use of this interpolator is not bean validation specification compliant 1.1.2.CD Bean Validation defines integration points with CDI (Contexts and Dependency Injection for Java M EE, JSR 346). If your application runs in an environment which does not provide this integration out of the box, you may use the Hibernate Validator CDI portable extension by adding the following Maven dependency to your POM Example 3. Hibernate validator CDI portable extension Maven dependen <dependency> <groupId>org. hibernates/groupIds <artifactIdshibernate-validator-cdis/artifactid> <version5.4.1. Final</version </dependency> Note that adding this dependency is usually not required for applications running on a Java eE application server. You can learn more about the integration of Bean validation and CDI in CDI 1.1.3. Running with a security manager Hibernate Validator supports running with a security manager being enabled To do so, you must assign several permissions to the hibernate validator and the bean validation API code bases. The following shows how to do this via a policy file as processed by the Java default policy implementation Example 4. Policy file for using hibernate validator with a security mand grant codeBase file: path/to/hibernate-validator-54 1. Final. jar( permission java, lang reflect. ReflectPermission suppressAccess checks permission java. lang. RuntimePermission " accessDeclaredMembers permission java. lang. RuntimePermission setContextClassloader // Only needed when working with XML descriptors (validation. xml or XML constraint mappings permission java util. Property Permission "mapAnyUriToUri",read grant codeBase " file: path/to/validation -api-110. Final. jar permission java. ioFilePermission "path/to/hibernate-validator- 5.4.1.Final. jar,read" All API invocations requiring special permissions are done via privileged actions. This means only Hibernate validator and the Bean Validation API themselves need the listed permissions. You don't need to assign any permissions to other code bases calling Hibernate validator. 1. 1. 4. Updating Hibernate validator in WildLy The Wild Fly application server contains Hibernate validator out of the box. In order to update the server modules for Bean Validation API and Hibernate validator to the latest and greatest the patch mechanism of WildLy can be used You can download the patch file from Source Forge or from maven Central using the following dependency Example 5 Maven dependency for wildLy 10.1.0. Final patch file ≤ dependency> <groupIdsorg hibernate</groupId> <artifactIdshibernate-validator-moduless/artifactid> <version>5.4.1. Final</version> <classifierswildfly-10.1.0. Final-patchs/classifier> ≤type>zip</type> </dependency> Having downloaded the patch file, you can apply it to Wild fly by running this command Example 6. Applying the wildLy patch SJBOSS_HOME/bin/jboss-cli sh patch apply hibernate-validator-modules- 5.4.1. Final-wildfly-10.1.0.Final-patch zip In case you want to undo the patch and go back to the version of Hibernate validator originally coming with the server, run the following command: Example 7. Rolling back the wildLy patch SJBOSS_HOME/bin/jboss-cli sh patch rollback --reset-configuration=true You can learn more about the Wild Fly patching infrastructure in general here and here 1.2. Applying constraints ets dive directly into an example to see how to apply constraints. Example 8 Class Car annotated with constraints package org. hibernate validator. referenceguide chapteral import javax. validation constraints. Min; import javax. validation, constraints. NotNull import javax. validation constraints. size; public class Car @NotNull private String manufacturer @NotNull @Size(min =2, max 14) private string licenseplate; @Min(2) private int seat Count; blic Car(String manufacturer, String licenceplate, int seat Count)( this, manufacturer manufacturer this, licenseplate licenceplate this. seatcount seatcoun t //getters and setters The @notNull. @size and amin annotations are used to declare the constraints which should be applied to the fields of a car instance manufacturer must never be null licensePlate must never be null and must be between 2 and 14 characters long seatCount must be at least 2 You can find the complete source code of all examples used in this reference guide in the Hibernate validator source repository on GitHub 1.3. Validating constraints To perform a validation of these constraints, you use a Validator instance. Lets have a look at a unit test for car EXample 9 Class Car Test showing validation examples package org. hibernate validator. referenceguide chapterol: t java util. Set import javax. validation. Constraintviolation import javax. validation Validation import javax. validation validator import javax. validation. ValidatorFactory: import org. junit. BeforeClass import org. junit Test import static org. junit Assert assertequals public class ArtEst private static validator validator @Be foreclass public static void setUpValidator()( ValidatorFactory factory Validation buildDefaultvalidatorFactory() validator factory. getValidator() @Test public void manufacturerIsNull(t Car car new Car( null,DD-AB-123 4) Set<Constraintviolation<car>> constraintviolations validator validate( car assertEquals( 1, constraintviolations, size()) assertequals("may not be null", constraintviolations iterator( next().getMessage())i

...展开详情
img

关注 私信 TA的资源

上传资源赚积分,得勋章
    最新推荐