在由Java JAXB注释类生成的模式中生成XSD限制 [英] Generation of XSD restrictions in a schema generated from Java JAXB annotated classes

查看:226
本文介绍了在由Java JAXB注释类生成的模式中生成XSD限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MOXy BeanValidation 让我能够为我的JAXB添加验证类。

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

使用MOXy的Bean Validation Plugin我可以在生成的JAXB类中基于 prexisting 架构的schema_facets.asprel =noreferrer> limits / facets

Using MOXy's "Bean Validation Plugin" I can have Bean Validation in generated JAXB classes based on restrictions/facets from a prexisting Schema.

但是有什么方法可以基于Bean验证注释,使用限制/方面生成架构 来自JAXB注释的java类

However is there any way of generating a schema with restrictions/facetsbased on Bean Validation annotations from a JAXB annotated java class?

XJC在执行'schema first'生成java时有一个方便的插件架构,但是有没有等效的' java first'的方式来增强生成的XSD带有附加限制,甚至添加XML注释?在MOXy或JAXB-RI中?

XJC has a handy plugin architecture when doing 'schema first' generating java, but is there any equivalent 'java first' way to enhance the generated XSD with additional restrictions, or even to add XML comments ? Either in MOXy or JAXB-RI?

MOXy非常灵活,可以在中间映射中使用,可以在模式生成期间使用吗?

MOXy is extremely flexible with meet in the middle mappings, can this be used during schema generation?

jaxb-facets项目似乎做了我想要的但是实施者不得不分叉一个全新的JAXB-RI来获取它,似乎它不会很快被采用。(参见此Java JIRA

The jaxb-facets project seems to do what I want but the implementer had to fork an entire new JAXB-RI to get it in and it seems that it won't be adopted any time soon.(See this Java JIRA)

我尝试了@ m0mus指定的分辨率但是使用sonatype存储库中的2.7.0-SNAPSHOT版本。我还有几个问题;
1.我必须使用@XmlElement注释java字段以使facet出现在xsd中。 @XmlAttribute,@ XmlAccessorType(XmlAccessType.FIELD)没有任何区别。 @Pattern没有用;我不得不在Pattern.List中使用单个Pattern处理;

I tried the resolution specified by @m0mus but had to use the 2.7.0-SNAPSHOT versions from the sonatype repository. I still had a couple of problems; 1. I had to annotate the java fields with @XmlElement to get the facets to appear in the xsd. @XmlAttribute, @XmlAccessorType(XmlAccessType.FIELD) made no difference. @Pattern did not work; I had to work around with a single Pattern in Pattern.List;

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

有关详细信息,请参阅 EclipseLink论坛

For more info see the EclipseLink Forum

推荐答案

我认为它在那儿。 MOXy使用自己的SchemaGen实现来处理从Java类生成XSD文件的过程。 SchemaGen已扩展为基于Java类上的BV注释自动生成XSD限制和构面。由于模式生成过程是在创建JAXBContext时发生的,因此可以通过在JAXBContext上设置以下属性(在JAXBContextProperties中找到)来打开/关闭BV增强功能:

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识别BV API提供的注释,包括@ Pattern.List。如果SchemaGen遇到一个用@NotNull和@XmlElement(nillable = true)注释的字段,它将引发BeanValidationException.notNullAndNillable()。

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().

示例:

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

这篇关于在由Java JAXB注释类生成的模式中生成XSD限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆