java JAXB 生成错误的类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14139957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 15:13:01  来源:igfitidea点击:

JAXB generates faulty classes

javaxsdjaxbjaxb2

提问by Siegfried Storr

after generating some classes from a xsd file, the classes are faulty. a @xmlElementRef is used with required="false" and that attribute is not allowed. My .xsd I'm using:

从 xsd 文件生成一些类后,这些类有问题。@xmlElementRef 与 required="false" 一起使用,该属性是不允许的。我正在使用的 .xsd:

<xs:schema xmlns:ns="http://shop.pau.softm.com/xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" 
targetNamespace="http://shop.pau.softm.com/xsd">
  <xs:element name="checkOrder">
  <xs:complexType>
  <xs:sequence>
            <xs:element minOccurs="0" name="order" nillable="true" type="ns:Order"/>
         </xs:sequence>
      </xs:complexType>
   </xs:element>
  <xs:complexType name="Order">
  <xs:sequence>
     <xs:element minOccurs="0" name="addressNumber" type="xs:int"/>
     <xs:element minOccurs="0" name="auftragsNummer" type="xs:int"/>
     <xs:element minOccurs="0" name="companyNumber" nillable="true" type="xs:string"/>
     <xs:element maxOccurs="unbounded" minOccurs="0" name="condition" nillable="true" type="ns:Condition"/>
     <xs:element minOccurs="0" name="currency" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="customerId" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="customerReference" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="deliveryAddress" nillable="true" type="ns:Address"/>
     <xs:element minOccurs="0" name="deliveryConditions" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="deliveryType" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="endCustomerId" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="endCustomerReference" nillable="true" type="xs:string"/>
     <xs:element maxOccurs="unbounded" minOccurs="0" name="error" nillable="true" type="ns:Error"/>
     <xs:element minOccurs="0" name="haltDescription" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="haltFlag" type="xs:boolean"/>
     <xs:element minOccurs="0" name="keySalesArea" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="openingHours" nillable="true" type="xs:string"/>
     <xs:element maxOccurs="unbounded" minOccurs="0" name="orderItem" nillable="true" type="ns:OrderItem"/>
     <xs:element minOccurs="0" name="origin" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="tourNumber" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="type" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="warehouseMan" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="warehouseNumber" type="xs:int"/>
     <xs:element minOccurs="0" name="wishedDeliveryDate" nillable="true" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="Condition">
  <xs:sequence>
     <xs:element minOccurs="0" name="description" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="type" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="value" nillable="true" type="xs:decimal"/>
  </xs:sequence>
</xs:complexType>
<xs:complexType name="Address">
<xs:sequence>
     <xs:element minOccurs="0" name="city" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="country" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="createAddress" type="xs:boolean"/>
     <xs:element minOccurs="0" name="name" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="name2" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="name3" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="name4" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="street" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="zip" nillable="true" type="xs:string"/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name="Error">
 <xs:sequence>
     <xs:element minOccurs="0" name="code" type="xs:int"/>
     <xs:element minOccurs="0" name="message" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="origin" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="type" nillable="true" type="xs:string"/>
  </xs:sequence>
 </xs:complexType>
 <xs:complexType name="OrderItem">
   <xs:sequence>
     <xs:element minOccurs="0" name="additionalText" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="additionalText2" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="customerProductId" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="eanCodeOrderUnit" nillable="true" type="xs:string"/>
     <xs:element maxOccurs="unbounded" minOccurs="0" name="error" nillable="true" type="ns:Error"/>
     <xs:element minOccurs="0" name="haltDescriptionItem" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="price" nillable="true" type="xs:decimal"/>
     <xs:element minOccurs="0" name="productId" nillable="true" type="xs:string"/>
     <xs:element minOccurs="0" name="quantity" type="xs:double"/>
    </xs:sequence>
</xs:complexType>
<xs:element name="checkOrderResponse">
   <xs:complexType>
    <xs:sequence>
        <xs:element minOccurs="0" name="return" nillable="true" type="ns:Order"/>
     </xs:sequence>
  </xs:complexType>
 </xs:element>
<xs:element name="submitOrder">
<xs:complexType>
<xs:sequence>
        <xs:element minOccurs="0" name="order" nillable="true" type="ns:Order"/>
     </xs:sequence>
  </xs:complexType>
 </xs:element>
<xs:element name="submitOrderResponse">
<xs:complexType>
<xs:sequence>
        <xs:element minOccurs="0" name="return" nillable="true" type="ns:Order"/>
     </xs:sequence>
  </xs:complexType>
</xs:element>
</xs:schema>

and the result (partly)

结果(部分)

package com.softm.pau.shop.xsd;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Address", propOrder = {
    "city",
    "country",
    "createAddress",
    "name",
    "name2",
    "name3",
    "name4",
    "street",
    "zip"
})
public class Address {

@XmlElementRef(name = "city", namespace = "http://shop.pau.softm.com/xsd", type =  JAXBElement.class, required = false)
protected JAXBElement<Object> city;
@XmlElementRef(name = "country", namespace = "http://shop.pau.softm.com/xsd", type = JAXBElement.class, required = false)
protected JAXBElement<Object> country;
protected Object createAddress;
@XmlElementRef(name = "name", namespace = "http://shop.pau.softm.com/xsd", type = JAXBElement.class, required = false)
protected JAXBElement<Object> name;
@XmlElementRef(name = "name2", namespace = "http://shop.pau.softm.com/xsd", type = JAXBElement.class, required = false)
protected JAXBElement<Object> name2;
@XmlElementRef(name = "name3", namespace = "http://shop.pau.softm.com/xsd", type = JAXBElement.class, required = false)
protected JAXBElement<Object> name3;
@XmlElementRef(name = "name4", namespace = "http://shop.pau.softm.com/xsd", type = JAXBElement.class, required = false)
protected JAXBElement<Object> name4;
@XmlElementRef(name = "street", namespace = "http://shop.pau.softm.com/xsd", type = JAXBElement.class, required = false)
protected JAXBElement<Object> street;
@XmlElementRef(name = "zip", namespace = "http://shop.pau.softm.com/xsd", type = JAXBElement.class, required = false)
protected JAXBElement<Object> zip;

How can i get XmlElement instead of XmlElementRef? What do i have to change in the xsd? The xsd is an extract from a wsdl and i want to use the resulting classes to call a webservice.

我怎样才能得到 XmlElement 而不是 XmlElementRef?我必须在 xsd 中更改什么?xsd 是 wsdl 的摘录,我想使用生成的类来调用 web 服务。

siegfried

齐格弗里德

OK, some mor infos here, can't answer my own question...

好的,这里有一些更多信息,无法回答我自己的问题...

I'm working with STS and Java6. in my build.xml is coded

我正在使用 STS 和 Java6。在我的 build.xml 中被编码

<!-- JAXB-->
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask">
    <classpath>
        <fileset dir="lib/ct" includes="*.jar" />
    </classpath>
</taskdef>

<target name="gen.java" description="Generates the JAXB business objects">
    <xjc destdir="src/gen" extension="true">
        <schema dir="conf/webapp/WEB-INF/schema" includes="*.xsd" />
        <binding dir="resources" includes="*.xjb" />
    </xjc>
</target>

My binding.xjb looks like

我的 binding.xjb 看起来像

<jxb:bindings version="1.0" 
 xmlns:jxb="http://java.sun.com/xml/ns/jaxb" 
 xmlns:s="http://www.w3.org/2001/XMLSchema" 
 xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
jxb:extensionBindingPrefixes="xjc">

<jxb:bindings schemaLocation="../conf/webapp/WEB-INF/schema/wsinapa.xsd" node="/s:schema">
    <jxb:bindings node="/s:schema/s:element[@name='AccountStatus']">
        <jxb:class name="AccountStatusRequest"/>
    </jxb:bindings>
</jxb:bindings>

in the src dir are 2 .xsd that should be compiled. One of them compiled without errors, the other not. Both where extracted from wsdl files.

在 src 目录中有 2 个 .xsd 应该被编译。其中一个编译没有错误,另一个没有。两者都是从 wsdl 文件中提取的。

cheers

干杯

Siegfried

齐格弗里德

回答by bdoughan

Your JAXB (JSR-222)implementation is not generating faulty classes. The requiredproperty was added to the @XmlElementRefannotation in JAXB 2.2.

您的JAXB (JSR-222)实现没有生成错误的类。该required属性已添加到@XmlElementRefJAXB 2.2 中的注释中。

Most likely you have generated you JAXB classes in Java SE 7 (which includes JAXB 2.2) and are trying to run then in Java SE 6 (which includes JAXB 2.1). Using a JAXB 2.2 implementation you can create JAXB 2.1 (or JAXB 2.0) compatible code using the -targetflag.

很可能您已经在 J​​ava SE 7(包括 JAXB 2.2)中生成了 JAXB 类,然后尝试在 Java SE 6(包括 JAXB 2.1)中运行。使用 JAXB 2.2 实现,您可以使用该-target标志创建 JAXB 2.1(或 JAXB 2.0)兼容代码。

xjc -target 2.1 schema.xsd

回答by Saravana Kumar

I think its nothing to change in xsd. Try creating one binding file as below, say simple-binding.xjb.

我认为在 xsd 中没有什么可改变的。尝试创建一个如下的绑定文件,比如 simple-binding.xjb。

<jaxb:bindings version="2.1" 
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
   <jaxb:globalBindings generateElementProperty="false"/> 
</jaxb:bindings> 

Then use this command when generating java class from xsd. I guess you are wsimportthat comes along with jdk for generating sources.

然后在从 xsd 生成 java 类时使用此命令。我猜你是wsimport用 jdk 来生成源代码的。

wsimport -b simple-binding.xjb

So you will not get the tag XmlElementRef

所以你不会得到标签 XmlElementRef

回答by Marek

In my project the same problem occurred because one of the fields of a type defined in a XSD file was duplicated. Therefore JAXB was unable to generate a proper java class because you cannot have two fields with the same name in a java class. As a workaround JAXB gave me the same weird output as in the original post.

在我的项目中,出现了同样的问题,因为 XSD 文件中定义的某个类型的字段被重复。因此 JAXB 无法生成正确的 java 类,因为在 java 类中不能有两个同名的字段。作为一种解决方法,JAXB 给了我与原始帖子中相同的奇怪输出。

I know this thread is quite old, but someone searching for a solution to a similar problem might find this helpful.

我知道这个线程已经很老了,但是寻找类似问题的解决方案的人可能会发现这很有帮助。

回答by jesseplymale

If you are using the maven-jaxb2-pluginto run XJC, you may run into this same problem. I fixed it by adding a <specVersion>of 2.1to my pom.xml. As follows:

如果您使用maven-jaxb2-plugin来运行 XJC,您可能会遇到同样的问题。我加入了固定它<specVersion>2.1到我pom.xml。如下:

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <executions>
    <execution>
      ...
      <configuration>
        ...
        <specVersion>2.1</specVersion>
        ...
      </configuration>
    </execution>
  </executions>
</plugin>