Generation of XSD restrictions in a schema generated from Java JAXB annotated classes

前端 未结 3 741
北海茫月
北海茫月 2021-01-13 10:56

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

Using MOXy\'s \"Bean Validation Plugin\" I can have Bean Validation in generated JAX

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-13 11:33

    I think it is there. MOXy uses its own SchemaGen implementation for the process of generating XSD files from Java classes. SchemaGen was extended to automatically generate XSD Restrictions and Facets based on BV annotations found on Java classes. Since the schema generation process takes place upon creation of JAXBContext, the BV enhancement feature can be turned on/off by setting the following property (found in JAXBContextProperties) on JAXBContext:

    /**
     * Property for disabling/enabling generation of XML Facets during schemagen.
     * The mapped value must be of type Boolean.
     * If it's 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 recognizes annotations provided by the BV API, including @Pattern.List. If SchemaGen encounters a field annotated with both @NotNull and @XmlElement(nillable = true), it will raise the BeanValidationException.notNullAndNillable().

    Sample:

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

提交回复
热议问题