JAXB生成错误的类

问题描述 投票:3回答:4

从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>

和结果(部分)

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;

我怎样才能获得XmlElement而不是XmlElementRef?我需要在xsd中更改什么? xsd是来自wsdl的摘录,我想使用结果类来调用web服务。

西格弗里德

好的,这里有更多信息,不能回答我自己的问题......

我正在使用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>

我的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>

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

干杯

齐格弗里德

java xsd jaxb jaxb2
4个回答
10
投票

您的JAXB (JSR-222)实现不会产生错误的类。 required属性被添加到JAXB 2.2中的@XmlElementRef注释中。

很可能你已经在Java 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

3
投票

我认为在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> 

然后在从xsd生成java类时使用此命令。我想你是wsimport与jdk一起出现来源。

wsimport -b simple-binding.xjb

所以你不会得到标签XmlElementRef


1
投票

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

我知道这个帖子已经很老了,但是有人在寻找类似问题的解决方案可能会觉得这很有帮助。


0
投票

如果您使用the maven-jaxb2-plugin来运行XJC,您可能会遇到同样的问题。我通过在<specVersion>添加2.1pom.xml来修复它。如下:

<plugin>
  <groupId>org.jvnet.jaxb2.maven2</groupId>
  <artifactId>maven-jaxb2-plugin</artifactId>
  <executions>
    <execution>
      ...
      <configuration>
        ...
        <specVersion>2.1</specVersion>
        ...
      </configuration>
    </execution>
  </executions>
</plugin>
© www.soinside.com 2019 - 2024. All rights reserved.