简体   繁体   English

创建具有复杂类型的Web服务

[英]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. 我是Web服务的新手,我使用一种公开的方法在eclipse中创建了一个基本项目。 I was able to deploy my webservice and it works fine. 我能够部署我的web服务,它工作正常。 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? 我是否需要额外的注释来创建WSDL? Thanks! 谢谢!

JAX-WS is based on JAXB so you can pass only JAXB supported types as a web method parameters. JAX-WS基于JAXB,因此您只能将JAXB支持的类型作为Web方法参数传递。 So any user defined class properly annotated such as mentioned below can be used as parameter or return type of any WebMethod 因此,任何正确注释的用户定义类(如下所述)都可以用作任何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 首先,设置Webservice调用或响应在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 在Java端,您需要一个包含这些包含getter和setter的字段的包装类

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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