Creating a web service with complex types
Asked Answered
B

2

6

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!

Bergstein answered 7/5, 2013 at 11:46 Comment(0)
L
15

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;
    }
}
Lathan answered 7/5, 2013 at 11:55 Comment(0)
S
1

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

Selfassurance answered 7/5, 2013 at 11:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.