Search code examples
javaweb-servicesjax-ws

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!


Solution

  • 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;
        }
    }