WebService Client and List

2020-07-20 04:26发布

问题:

When I try to generate a client from a wsdl document, I get a client that seems to have a lot of JAXBElement atributes, for instance

protected List<JAXBElement<?>> nameOrLinkingNameOrFamilyName;

I use soapUI to generate with apache cxf 2.3.3 as tool, also as config file the following:

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

As far as I saw this is related with the choice tags in the wsdl document.

thanks in advance

回答1:

A JAXBElement will be generated for choice properties where multiple XML elements will correspond to the same Java class. This is in order to preserve information about the element since this can not be derived from the type of the value.

binding.xml

The following JAXB schema bindings file will ensure that a choice property is generated:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          version="2.1">
    <globalBindings choiceContentProperty="true"/>
</bindings> 

XML Schema That Will Produce Object Property

In this version of the XML schema, all of the XML elements will correspond to a different Java class:

<xsd:choice>
    <xsd:element name="address" type="address"/>
    <xsd:element name="phone-number" type="phoneNumber"/>
    <xsd:element name="note" type="xsd:string"/>
</xsd:choice>

Since the value of the choice property is sufficient to uniquely identify the element, the property does not contain a JAXBElement to preserve this information:

@XmlElements({
    @XmlElement(name = "address", type = Address.class),
    @XmlElement(name = "phone-number", type = PhoneNumber.class),
    @XmlElement(name = "note", type = String.class)
})
protected Object addressOrPhoneNumberOrNote;

XML Schema That Will Produce JAXBElement Property

Now we will modify the choice structure so that both the note and email methods will correspond to the String class.

<xsd:choice>
    <xsd:element name="address" type="address"/>
    <xsd:element name="phone-number" type="phoneNumber"/>
    <xsd:element name="note" type="xsd:string"/>
    <xsd:element name="email" type="xsd:string"/>
</xsd:choice>

Since the value of the choice property is no longer sufficient to uniquely identify the element, the property must contain a JAXBElement to preserve this information:

@XmlElementRefs({
    @XmlElementRef(name = "phone-number", type = JAXBElement.class),
    @XmlElementRef(name = "email", type = JAXBElement.class),
    @XmlElementRef(name = "address", type = JAXBElement.class),
    @XmlElementRef(name = "note", type = JAXBElement.class)
})
protected JAXBElement<?> addressOrPhoneNumberOrNote;

For More Information

  • http://blog.bdoughan.com/2011/04/xml-schema-to-java-xsd-choice.html