简体   繁体   中英

Creating a web service with complex types

I'm new to web services and I created a basic project in eclipse with one exposed method. I was able to deploy my webservice and it works fine. The code is below.

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService(targetNamespace="http://test.com", name="testService")
public class WebService {
    @WebMethod(operationName="start")
    public String start(@WebParam(name="inputParameter") String inputParameter) {
        return startMethod(inputParameter);
    }
}

My question is how do I set up this method to deal with complex types. I want to receive a number of parameters, but I don't want to just receive them as a bunch of strings. I was thinking of having some sort of wrapper object that contained all the parameters I need for my method. Any advice on how to do this? Do I need additional annotations to create the WSDL? Thanks!

JAX-WS is based on JAXB so you can pass only JAXB supported types as a web method parameters. So any user defined class properly annotated such as mentioned below can be used as parameter or return type of any WebMethod

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Person")
public class Person {    
    @XmlElement(name = "firstName")
    protected String firstName;    
    @XmlElement(name = "lastName")
    protected String lastName;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String value) {
        this.firstName = value;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String value) {
        this.lastName = value;
    }
}

First, setup what complex types your webservice call or response contains in your WSDL

<xsd:element name="AWebServiceElementName">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element maxOccurs="1" minOccurs="1" name="header" type="tns:ReplyHeader"/>
                        <xsd:element maxOccurs="1" minOccurs="1" name="body">
                            <xsd:complexType>
                                <xsd:sequence>
                                    <xsd:element maxOccurs="unbounded" minOccurs="0" name="acomplextype" type="tns:acomplextype"/>
                                    <xsd:element maxOccurs="1" minOccurs="1" name="anothercomplextype" type="tns:anothercomplextype"/>
                                </xsd:sequence>
                            </xsd:complexType>
                        </xsd:element>
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

and then define what your complex types contains:

        <xsd:complexType name="acomplextype">
            <xsd:sequence>
                <xsd:element maxOccurs="1" minOccurs="1" name="somefieldid" type="xsd:long"/>
                <xsd:element maxOccurs="1" minOccurs="1" name="somestring" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>
        <xsd:complexType name="anothercomplextype">
            <xsd:sequence>
                <xsd:element maxOccurs="1" minOccurs="1" name="somefieldid" type="xsd:long"/>
                <xsd:element maxOccurs="1" minOccurs="1" name="somestring" type="xsd:string"/>
            </xsd:sequence>
        </xsd:complexType>

On the Java-side you need a wrapper class that contain these fields with getters and setters

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