Creating a Web Service with Complex Types

I am new to web services and I created a basic project in eclipse with one open method. I was able to deploy my web service and it works great. 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 to configure this method to handle complex types. I want to get some parameters, but I don't want to just get them as a bunch of strings. I was thinking about having some kind of wrapper object containing all the parameters that I need for my method. Any tips on how to do this? Are additional annotations needed to create a WSDL? Thanks!

+6
source share
2 answers

JAX-WS is based on JAXB, so you can only pass JAXB-supported types as parameters to the web method. Thus, any custom class properly annotated, such as the one mentioned below, can be used as a 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; } } 
+15
source

First, indicate what complex types of your web service call or response are 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 determine what your complex types contain:

  <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 contains these fields using getters and seters

+1
source

Source: https://habr.com/ru/post/944438/


All Articles