Siegfried Storr
Siegfried Storr

Reputation: 67

JAXB generates faulty classes

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:

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

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

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

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

cheers

Siegfried

Upvotes: 3

Views: 12319

Answers (4)

Marek
Marek

Reputation: 11

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.

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

Upvotes: 1

jesseplymale
jesseplymale

Reputation: 704

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

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

Upvotes: 0

bdoughan
bdoughan

Reputation: 148977

Your JAXB (JSR-222) implementation is not generating faulty classes. The required property was added to the @XmlElementRef annotation in JAXB 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 -target flag.

xjc -target 2.1 schema.xsd

Upvotes: 10

Saravana Kumar
Saravana Kumar

Reputation: 113

I think its nothing to change in xsd. Try creating one binding file as below, say 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 wsimport that comes along with jdk for generating sources.

wsimport -b simple-binding.xjb

So you will not get the tag XmlElementRef

Upvotes: 4

Related Questions