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

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

问题描述

MOXy BeanValidation 让我能够向我的 JAXB 添加验证类.

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

使用 MOXy 的Bean 验证插件",我可以根据 restrictions/在生成的 JAXB 类中进行 Bean 验证来自现有架构的方面.

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

但是有什么方法可以生成模式 restrictions/facets基于 Bean Validation 注释来自 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 在模式优先"生成 java 时有一个方便的插件架构,但是是否有任何等效的java 优先"方法来增强生成的 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 字段以使构面出现在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天全站免登陆