XJC Plugin to generate Bean Validation Annotations 2.0 (JSR-380)
It works with (and possibly other plugins using xjc
):
maven-jaxb2-plugin
see krasa-maven-jaxb2-plugin-examplejaxb2-maven-plugin
see krasa-jaxb2-maven-plugin-examplecxf-codegen-plugin
see: krasa-cxf-codegen-plugin-examplecxf-xjc-plugin
see krasa-cxf-xjc-plugin-example
Refer to the krasa-jaxb-tools-example
project for usage examples.
The project is bounded to support Java 8 (JDK 1.8) because of some old projects still requiring it. All dependencies are selected from the latest available versions still supporting that.
Check krasa-issue13 if you need an example of a project using krasa-jaxb-tools
with JDK 21
, cxf-codegen-plugin
4.0.4 and all the latest dependencies.
-
2.3.5
fix critical vulnerabilities found in dependencies, see Sonatype report. -
2.3.4
bug fix release:- fix Issue #17 where
@DecimalMin
and@DecimalMax
superfluous annotations were added to numeric java types. A new argument has been createdgenerateAllNumericConstraints
in case all constraints would be needed (even superflous ones).
- fix Issue #17 where
-
2.3.3
bug fix release:- fix Issue #13 where it's been wrongly assumed that:
SimpleTypeImpl particle = (SimpleTypeImpl) definition
was always true. A check has beed added to prevent theClassCastException
.
- fix Issue #13 where it's been wrongly assumed that:
-
2.3.2
another bug fix release:- fix
@Pattern
added to wrong fields (regression from 2.2) - add
@EachPattern
when needed forList<String>
fields - rename
generateStringListAnnotations
option togenerateListAnnotations
because it is not limited to list of strings - disable
generateListAnnotations
by default (was enabled)
- fix
-
2.3.1
bug fix release:@Valid
annotation was not added by default- remove
singlePattern
option because@Pattern.List
is not semantically correct - disable
jpa
option because not really useful - disable
JSR_349
option it was referring to Validation API 1.1 while now we use 2.0 - add a lot of tests to establish a solid baseline (defaults was backported and tested on 2.2)
-
2.3
A huge refactoring and bug fixing:- added
singlePattern
option - fixed
generateServiceValidationAnnotations
used byValidSEIGenerator
to accept string parameter - dependencies updated to the latest version still supporting JDK 1.8
- a maven rule has been set to force compilation with JDK 1.8
- added
-
2.2
Some new features added because of PR requests- Added
@Valid
annotation tosequence
s to force items validation - Added support for
Jakarta EE 9
with parametervalidationAnnotations
- Added
-
2.1
Revert back to Java 1.8 (sorry folks!). -
2.0
A refactorized version of the original krasa-jaxb-toos last synced on August 2022, with some enhancements (support forEachDigits
,EachDecimalMin
andEachDecimalMax
in primitive lists), improved tests and bug fixed. It is compiled using JDK 11. Thepom.xml
groupId
has been changed tocom.fillumina
.
<dependency>
<groupId>com.fillumina</groupId>
<artifactId>krasa-jaxb-tools</artifactId>
<version>2.3.5</version>
</dependency>
verbose
(boolean, default=false
) print verbose messages to outputvalidationAnnotations
(javax
|jakarta
, default=javax
): selects the library to use for annotationstargetNamespace
(string): adds @Valid annotation to all elements with given namespacegenerateNotNullAnnotations
(boolean, default=true
): adds a@NotNull
annotation if an element hasminOccours
not 0, isrequired
or is notnillable
.notNullAnnotationsCustomMessages
(boolean or string, default=false
): values aretrue
,FieldName
,ClassName
, or an actual message (see further explanation down below).generateListAnnotations
(boolean, optional, defaultfalse
) generates validator-collection annotations annotationsgenerateServiceValidationAnnotations
(string, accepts:in
,out
,inout
, works withapache-cxf
only) adds@Valid
annotations to respective message direction (in, out or both).generateAllNumericConstraints
(boolean, defaults tofalse
) generates all@DecimalMin
and@DecimalMax
even those regarding the natural boudaries of the referred java type.
-
Arguments accepting booleans can either be given
true
value as withverbose=true
or simply be left without a value at all and that will be interpteted as beingtrue
(you can omit the=
too). -
All arguments are optional.
@NotNull
's default validation message is not always helpful, so it can be customized with -XJsr303Annotations:notNullAnnotationsCustomMessages=OPTION where OPTION is one of the following:
false
default: no custom messagetrue
message is present but equivalent to the default: "{javax.validation.constraints.NotNull.message}"FieldName
field name is prefixed to the default message: "fieldName {javax.validation.constraints.NotNull.message}"ClassName
class and field name are prefixed to the default message: "ClassName.fieldName {javax.validation.constraints.NotNull.message}"other-non-empty-text
arbitrary message, with substitutable, case-sensitive parameters{ClassName}
and{FieldName}
: "Class {ClassName} field {FieldName} non-null"
Bean validation policy can be customized with -XJsr303Annotations:generateServiceValidationAnnotations=OPTION
where OPTION is one of the following:
InOut
(default: validate requests and responses)In
(validate only requests)Out
(validate only responses)
Using this option requires to specify krasa as front end generator (See example in https://github.com/fillumina/krasa-jaxb-tools-example )
That is a different plugin within this same packakge and replaces primitive types with boxed ones. It's enabled in the krasa-cxf-codegen-plugin-example project as an example. WARNING: must be defined before XhashCode or Xequals.
Generates:
@Valid
annotation for all complex types, can be further restricted to generate only for types from defined schema: -XJsr303Annotations:targetNamespace=http://www.foo.com/bar@NotNull
annotation for objects that has a MinOccur value >= 1 or for attributes with required use@Size
for lists that have minOccurs > 1@Size
if there is a maxLength or minLength or length restriction@DecimalMax
for maxInclusive restriction@DecimalMin
for minInclusive restriction@DecimalMax
for maxExclusive restriction, enable new parameter (inclusive=false) with: -XJsr303Annotations:JSR_349=true@DecimalMin
for minExclusive restriction, enable new parameter (inclusive=false) with: -XJsr303Annotations:JSR_349=true@Digits
if there is a totalDigits or fractionDigits restriction.@Pattern
and@PatternList
if there is a Pattern restriction (seesinglePattern
option)
This other project is maintained to allow testing and showcase usage:
https://github.com/fillumina/krasa-jaxb-tools-example
Note that the JDK 8 compatibility requirement impose serious restrictions on the dependency versions available so it is highly advisable to check the used versions and general usage carefully in this test project.