Skip to main content

JAXB plugin to generate Bean Validation Annotations (JSR-303)

2 replies [Last post]
cocorossello
Offline
Joined: 2009-01-12
Points: 0

Hi,

I did this plugin myself to map xs:restrictions to the objects generated by the XJC compiler.

What it does:

-It generates @valid annotation for objects that are not in the xs default schema (so annotations are cascaded)

-It generates @NotNull annotation for objects that has a MinOccur value >= 1 or for attributes with required use

-It generates @Size for lists that have minOccurs > 1

-It generates @Size if there is a maxLength or minLength restriction

-@DecimalMax for maxInclusive restriction

-@DecimalMin for minInclusive restriction

-@Digits if there is a totalDigits or fractionDigits restriction.

-@Pattern if there is a Pattern restriction

Please note that minExclusive and maxExclusive restrictions are excluded.

To use it, you have to package the class file along a META-INF/services/com.sun.tools.xjc.Plugin file with the content "com.sun.tools.xjc.addon.jaxb.JaxbValidationsPlugins" (that is, the fully qualified name of the class) and call XJC with the -XValidate switch.

It's not really difficult to implement, but I hope it will be useful for someone. The source code is attached as a TXT file. Enjoy!

AttachmentSize
JaxbValidationsPlugins.txt10.72 KB

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
nothingspecial
Offline
Joined: 2012-09-21
Points: 0

Great work!
I little fixed it for usage with cxf-codegen-plugin, created maven module with some tests and hosted it at http://code.google.com/p/krasa-jaxb-tools/

snajper
Offline
Joined: 2004-10-01
Points: 0

Hi,
sorry for late response ... would you like to host your plugin under
jaxb2-commons project?
MartiNG

On 3/27/12 5:14 PM, forums@java.net wrote:
> Hi,
>
> I did this plugin myself to map xs:restrictions to the objects
> generated by
> the XJC compiler.
>
> What it does:
>
> -It generates @valid annotation for objects that are not in the xs
> default
> schema (so annotations are cascaded)
>
> -It generates @NotNull annotation for objects that has a MinOccur
> value >= 1
> or for attributes with required use
>
> -It generates @Size for lists that have minOccurs > 1
>
> -It generates @Size if there is a maxLength or minLength restriction
>
> -@DecimalMax for maxInclusive restriction
>
> -@DecimalMin for minInclusive restriction
>
> -@Digits if there is a totalDigits or fractionDigits restriction.
>
> -@Pattern if there is a Pattern restriction
>
>
>
> Please note that minExclusive and maxExclusive restrictions are excluded.
>
> To use it, you have to package the class file along a
> META-INF/services/com.sun.tools.xjc.Plugin file with the content
> "com.sun.tools.xjc.addon.jaxb.JaxbValidationsPlugins" (that is, the
> fully
> qualified name of the class) and call XJC with the -XValidate switch.
>
> It's not really difficult to implement, but I hope it will be useful for
> someone. The source code is attached as a TXT file. Enjoy!
>
>
>
>