Generating XSD Constraints in a Schema Generated from Java JAXB Annotated Classes

MOXy BeanValidation gives me the ability to add validation to my JAXB classes.

Using MOXy "Bean Validation Plugin", I can have Bean Validation in generated JAXB classes based on the constraints / facets of an existing schema.

However, is there a way to create a constrained / facet schema based on Bean Validation annotations from a JAXB annotated Java class?

XJC has a convenient plug-in architecture when creating a “schema first” generating Java, but is there an equivalent “Java first” way to improve the generated XSD with additional restrictions or even add XML comments? Either in MOXI or in JAXB-RI?

MOXy is extremely flexible in the middle, can this be used to generate the circuit?

It looks like the jaxb-facets project is doing what I want, but the developer had to fork out for the whole new JAXB-RI, and it looks like it won't be adopted any time soon ( see Java JIRA ).


I tried the resolution indicated by @ m0mus, but I had to use version 2.7.0-SNAPSHOT from the sonatype repository. I still had a couple of problems; 1. I had to annotate Java fields using @XmlElement so that facets appear in xsd. @XmlAttribute, @XmlAccessorType (XmlAccessType.FIELD) does not matter. @ The pattern did not work; I had to work with one Pattern in Pattern.List;

@XmlElement
@Pattern.List(value = { @Pattern(regexp="[0-9]*") })
public String phoneNumber2;

See the EclipseLink Forum for more information.

+5
source share
3 answers

, . MOXy SchemaGen XSD Java. SchemaGen , XSD , BV, Java-. JAXBContext, BV / ( JAXBContextProperties) JAXBContext:

/**
 * Property for disabling/enabling generation of XML Facets during schemagen.
 * The mapped value must be of type Boolean.
 * If it true, then facets will be generated, based on the BV annotations.
 * If false, the BV annotations processing will be skipped during schemagen
 * and no facets will be generated.
 *
 * @since 2.6
 */
public static final String GENERATE_FACETS = "eclipselink.generate.facets";

SchemaGen , API BV, @Pattern.List. SchemaGen , @NotNull, @XmlElement (nillable = true), BeanValidationException.notNullAndNillable().

:

Map props = new HashMap( );
props.put("eclipselink.beanvalidation.facets", true);
JAXBContext jc = JAXBContext.newInstance(classes, properties);
SchemaOutputResolver sor = new MSOR();
jc.generateSchema(sor);
+2

@XMLAttribute:

, v2.7, , facate enable . 5 . EL v2.6 .

+3

xjc https://github.com/krasa/krasa-jaxb-tools

XJsr303Annotations :

  • @Valid : -XJsr303Annotations:targetNamespace=http://www.foo.com/bar
  • @NotNull MinOccur > = 1
  • @Size , minOccurs > 1
  • @Size maxLength minLength
  • @DecimalMax maxInclusive
  • @DecimalMin minInclusive
  • @DecimalMax maxExclusive, (inclusive=false) : -XJsr303Annotations:JSR_349=true
  • @DecimalMin minExclusive, (inclusive=false) : -XJsr303Annotations:JSR_349=true
  • @Digits totalDigits fractionDigits.
  • @Pattern Pattern

, XJC (ant, maven, gradle), : immutable-xjc

, .

0

Source: https://habr.com/ru/post/1605978/


All Articles