Validation for generated JAXB Classes (JSR 303 / Spring)

…衆ロ難τιáo~ 提交于 2020-01-22 19:22:27

问题


I Generated domain objects from schema (request & response) using JAXB (maven-jaxb2-plugin)
I would like add validations (notnull /empty) for couple of attributes. I would like to have custom Bean Validation, the application is a REST service, i'm using Spring 3 and JSR 303 but i dont think i can use JSR 303 to validate the object as it is generated from the schema.

can someone give me a nudge in the right direction on how to get this done.


回答1:


We've been using the Krasa JAXB plugin to generate JSR 303-annotated model beans from XSD files, and then telling Spring to automatically validate the input beans. This results in very nice, very terse, very DRY code if you have good XSDs.




回答2:


You can, indeed, do this, via jsr-303 xml configuration. See, for example, http://www.aviyehuda.com/2010/04/using-hibernate-validator-to-cover-your-validation-needs/.




回答3:


For NotNull/Empty validation you can use jaxb restriction in schema

here is an example:

<xsd:simpleType name="NotEmptyString">
  <xsd:restriction base="xsd:string">
    <xsd:minLength  value="1"/>
  </xsd:restriction>
</xsd:simpleType>

Or you can use regular expression patterns:

<xsd:simpleType name="DirType">
  <xsd:restriction base="xsd:string">
    <xsd:pattern value="[LR]*"/>
  </xsd:restriction>
</xsd:simpleType>

Note: the simple type doesn't warrant a class definition of its own. Java's own java.lang.String is used, and the length restriction isn't checked unless you request it via setEventHandler() .

more info http://jaxb.java.net/tutorial/section_3_3-Validation.html#Validation




回答4:


You are on the right path to generate the JAXB. Click Here to see How JSR 303 Works with POC



来源:https://stackoverflow.com/questions/5838133/validation-for-generated-jaxb-classes-jsr-303-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!