如何强制生成@XmlSeeAlso注释?(How to force generation of @XmlSeeAlso annotation?)

编程入门 行业动态 更新时间:2024-10-24 14:20:37
如何强制生成@XmlSeeAlso注释?(How to force generation of @XmlSeeAlso annotation?)

我正在使用JAXB从几个XSD文件生成Java代码。 然后,在OSGi容器内部,我将XML文件解组为生成的代码。 XSD使用xsd:any元素:

<xsd:complexType name="GetReservationRSType"> <xsd:sequence> <xsd:element name="Errors" type="pnrb:Errors.PNRB" minOccurs="0" /> <xsd:choice> <xsd:element name="Reservation" type="pnrb:Reservation.PNRB" minOccurs="0" /> <xsd:element name="Content" minOccurs="0"> <xsd:complexType> <xsd:choice> <xsd:any processContents="lax" /> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:sequence> </xsd:complexType>

我在生产代码中使用它时遇到了一些问题,但最终我在下面的代码中手动添加@XmlSeeAlso注释( @XmlSeeAlso(value = { OTATravelItineraryRS.class })了这个问题):

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "GetReservationRSType", propOrder = { "warnings", "errors", "reservation", "content" }) @XmlSeeAlso({ GetReservationRS.class }) public class GetReservationRSType { @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "any" }) @XmlSeeAlso(value = { OTATravelItineraryRS.class }) public static class Content { // ... } // ... }

有什么办法可以强制JAXB自动添加这样的注释吗? 例如,通过添加一些JAXB绑定配置选项或修改XSD文件?

编辑:

我在OSGi env中的JAXBContext以下面的方式初始化(​​它将所有生成的包名称作为参数) - 它们列出了冒号分隔符的用法,因为它在几个与JAXB相关的帖子中提出:

<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance"> <constructor-arg index="0" value="com.sabre.webservices.pnrbuilder:com.sabre.webservices.sabrexml._2003._07" /> </bean>

我收到以下异常:

Caused by: javax.xml.bind.MarshalException - with linked exception: [javax.xml.bind.JAXBException: class com.sabre.webservices.sabrexml._2003._07.OTATravelItineraryRS nor any of its super class is known to this context.] at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:318) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:105) at org.apache.camel.converter.jaxb.FallbackTypeConverter.marshall(FallbackTypeConverter.java:174) at org.apache.camel.converter.jaxb.FallbackTypeConverter.convertTo(FallbackTypeConverter.java:88) ... 94 more Caused by: javax.xml.bind.JAXBException: class com.sabre.webservices.sabrexml._2003._07.OTATravelItineraryRS nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:246) at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:261) at com.sun.xml.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(SingleReferenceNodeProperty.java:113) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:332) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:699) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:152) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:332) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:328) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:593) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:320) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315) ... 98 more Caused by: javax.xml.bind.JAXBException: class com.sabre.webservices.sabrexml._2003._07.OTATravelItineraryRS nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:590) at com.sun.xml.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(SingleReferenceNodeProperty.java:105) ... 107 more

I am using JAXB to generate Java code from a couple of XSD files. Then, inside of an OSGi container I am unmarshalling XML files to the generated code. The XSD uses xsd:any element:

<xsd:complexType name="GetReservationRSType"> <xsd:sequence> <xsd:element name="Errors" type="pnrb:Errors.PNRB" minOccurs="0" /> <xsd:choice> <xsd:element name="Reservation" type="pnrb:Reservation.PNRB" minOccurs="0" /> <xsd:element name="Content" minOccurs="0"> <xsd:complexType> <xsd:choice> <xsd:any processContents="lax" /> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:sequence> </xsd:complexType>

I had several problems with making it working in the production code, but eventually I solved it when I manually added @XmlSeeAlso annotation (@XmlSeeAlso(value = { OTATravelItineraryRS.class }) in the code below):

@XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "GetReservationRSType", propOrder = { "warnings", "errors", "reservation", "content" }) @XmlSeeAlso({ GetReservationRS.class }) public class GetReservationRSType { @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "any" }) @XmlSeeAlso(value = { OTATravelItineraryRS.class }) public static class Content { // ... } // ... }

Is there any way I can force JAXB to automatically add such annotation? For instance, by adding some JAXB bindings configuration option or by modifying XSD files?

EDIT:

My JAXBContext in OSGi env is initialized in the following way (it gets all the generated packages names as parameter) - they are listed with the usage of colon delimiter as it was suggested in several JAXB-related posts:

<bean id="jaxbContext" class="javax.xml.bind.JAXBContext" factory-method="newInstance"> <constructor-arg index="0" value="com.sabre.webservices.pnrbuilder:com.sabre.webservices.sabrexml._2003._07" /> </bean>

I am getting the following exception:

Caused by: javax.xml.bind.MarshalException - with linked exception: [javax.xml.bind.JAXBException: class com.sabre.webservices.sabrexml._2003._07.OTATravelItineraryRS nor any of its super class is known to this context.] at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:318) at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:244) at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:105) at org.apache.camel.converter.jaxb.FallbackTypeConverter.marshall(FallbackTypeConverter.java:174) at org.apache.camel.converter.jaxb.FallbackTypeConverter.convertTo(FallbackTypeConverter.java:88) ... 94 more Caused by: javax.xml.bind.JAXBException: class com.sabre.webservices.sabrexml._2003._07.OTATravelItineraryRS nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:246) at com.sun.xml.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:261) at com.sun.xml.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(SingleReferenceNodeProperty.java:113) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:332) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:699) at com.sun.xml.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:152) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:332) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:328) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:593) at com.sun.xml.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:320) at com.sun.xml.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:494) at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:315) ... 98 more Caused by: javax.xml.bind.JAXBException: class com.sabre.webservices.sabrexml._2003._07.OTATravelItineraryRS nor any of its super class is known to this context. at com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo(JAXBContextImpl.java:590) at com.sun.xml.bind.v2.runtime.property.SingleReferenceNodeProperty.serializeBody(SingleReferenceNodeProperty.java:105) ... 107 more

最满意答案

我找到了令人满意的解决方法。 它使用jaxb annotate插件 。 也许它不是完美的解决方案,但是 - 至少 - 它阻止我将生成的类提交到SVN存储库中。 上面提到的插件只是添加了想要的注释。

这是pom.xml中所需的配置:

<plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <executions> <execution> <id>schema-pnr-service</id> <goals> <goal>generate</goal> </goals> <configuration> <schemaDirectory>src/main/resources/xsd</schemaDirectory> <schemaIncludes> <include>GetReservationSTLRQ_v0.01.xsd</include> <include>GetReservationSTLRS_v0.01.xsd</include> <include>OTA_TravelItineraryReadCDI1.0.5RQ.xsd</include> <include>OTA_TravelItineraryReadCDI1.0.5RQRS.xsd</include> <include>OTA_TravelItineraryReadCDI1.0.5RS.xsd</include> <include>OTA_TravelItineraryReadPNR1.0.5RS.xsd</include> </schemaIncludes> <extension>true</extension> <args> <arg>-Xannotate</arg> <arg>-Xannotate-defaultFieldTarget=setter</arg> </args> <plugins> <plugin> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics-annotate</artifactId> </plugin> </plugins> </configuration> </execution> </executions> </plugin>

想要的注释在bindings.xjb文件中配置,该文件应与* .xsd文件位于同一目录中 - 这里是其内容:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" xmlns:annox="http://annox.dev.java.net" extensionBindingPrefixes="annox" version="2.1"> <globalBindings typesafeEnumMaxMembers="600"/> <bindings schemaLocation="GetReservationSTLRS_v0.01.xsd" node="/xsd:schema"> <bindings node="xsd:complexType[@name='GetReservationRSType']/xsd:sequence/xsd:choice/xsd:element[@name='Content']/xsd:complexType"> <annox:annotate target="class"> <annox:annotate annox:class="javax.xml.bind.annotation.XmlSeeAlso" value="com.sabre.webservices.sabrexml._2003._07.OTATravelItineraryRS" /> </annox:annotate> </bindings> </bindings> </bindings>

I found a satisfying workaround. It uses jaxb annotate plugin. Maybe it's not the perfect solution, but - at least - it prevents me from committing the generated classes into SVN repository. The mentioned plugin simply adds the wanted annotation.

Here is the required configuration in pom.xml:

<plugin> <groupId>org.jvnet.jaxb2.maven2</groupId> <artifactId>maven-jaxb2-plugin</artifactId> <executions> <execution> <id>schema-pnr-service</id> <goals> <goal>generate</goal> </goals> <configuration> <schemaDirectory>src/main/resources/xsd</schemaDirectory> <schemaIncludes> <include>GetReservationSTLRQ_v0.01.xsd</include> <include>GetReservationSTLRS_v0.01.xsd</include> <include>OTA_TravelItineraryReadCDI1.0.5RQ.xsd</include> <include>OTA_TravelItineraryReadCDI1.0.5RQRS.xsd</include> <include>OTA_TravelItineraryReadCDI1.0.5RS.xsd</include> <include>OTA_TravelItineraryReadPNR1.0.5RS.xsd</include> </schemaIncludes> <extension>true</extension> <args> <arg>-Xannotate</arg> <arg>-Xannotate-defaultFieldTarget=setter</arg> </args> <plugins> <plugin> <groupId>org.jvnet.jaxb2_commons</groupId> <artifactId>jaxb2-basics-annotate</artifactId> </plugin> </plugins> </configuration> </execution> </executions> </plugin>

And the wanted annotation is configured in in bindings.xjb file that should be in the same directory as *.xsd files - here goes its content:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" xmlns:annox="http://annox.dev.java.net" extensionBindingPrefixes="annox" version="2.1"> <globalBindings typesafeEnumMaxMembers="600"/> <bindings schemaLocation="GetReservationSTLRS_v0.01.xsd" node="/xsd:schema"> <bindings node="xsd:complexType[@name='GetReservationRSType']/xsd:sequence/xsd:choice/xsd:element[@name='Content']/xsd:complexType"> <annox:annotate target="class"> <annox:annotate annox:class="javax.xml.bind.annotation.XmlSeeAlso" value="com.sabre.webservices.sabrexml._2003._07.OTATravelItineraryRS" /> </annox:annotate> </bindings> </bindings> </bindings>

更多推荐

本文发布于:2023-08-01 02:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1351733.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:注释   XmlSeeAlso   force   annotation   generation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!