简体   繁体   中英

Use generated JAXB class with lists in web servise wsdl/xsd

I have class definition in xsd file. I generated class using JAXB binding. For lists I haven't setters.

After that, I used this classes in my web services. I created wsdl + xsd for service. But ... in xsd I haven't some field definition (for lists, because I haven't setters for these).

How can I generate correctly wsdl + xsd for my classes?

Netbeans 7.3.1 + Glassfish

Sample code: Base XSD:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<xs:element name="A">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" name="B">
                <xs:complexType>
                    <xs:sequence>
                        <xs:element name="FIELD1" type="xs:dateTime">
                        </xs:element>
                    </xs:sequence>
                </xs:complexType>
            </xs:element>
        </xs:sequence>
    </xs:complexType>
</xs:element>

I do JAX binding, so It generated class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "b"
})
@XmlRootElement(name = "A")
public class A {

@XmlElement(name = "B", required = true)
protected List<A.B> b;

public List<A.B> getB() {
    if (b == null) {
        b = new ArrayList<A.B>();
    }
    return this.b;
}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "field1"
})
public static class B {

    @XmlElement(name = "FIELD1", required = true)
    @XmlSchemaType(name = "dateTime")
    protected XMLGregorianCalendar field1;

    public XMLGregorianCalendar getFIELD1() {
        return field1;
    }

    public void setFIELD1(XMLGregorianCalendar value) {
        this.field1 = value;
    }
}

}

As You can see, there is no setter for b field...

And I use it in web service:

@WebMethod(operationName = "hello")
public String hello(@WebParam(name = "name") A txt) {
    return "Hello " + txt + " !";
}

So finnaly, I have wsdl + xsd, with A class, but with no field:

  <xs:complexType name="a"><xs:sequence/>
  </xs:complexType>

JAXB doesn't generate setters for Collections. You can access your list with its getter then use the list methods to modify it.

Example:

txt.getB().add(new B());

Source: See here for details.

EDIT due to comment:

After further analysis of the question, I think what you're looking for is the JiBX's BindGen Tool . It is a "tool used to generate a binding definition and matching schema definition from existing Java code".

Hope this helps you further with your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM