問題描述
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 限制的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!