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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16418206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:49:14  来源:igfitidea点击:

Creating a web service with complex types

javaweb-servicesjax-ws

提问by yellavon

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.

我是 Web 服务的新手,我使用一种公开的方法在 Eclipse 中创建了一个基本项目。我能够部署我的网络服务并且运行良好。代码如下。

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!

我的问题是如何设置此方法来处理复杂类型。我想接收许多参数,但我不想只将它们作为一堆字符串接收。我正在考虑拥有某种包装对象,其中包含我的方法所需的所有参数。关于如何做到这一点的任何建议?我是否需要额外的注释来创建 WSDL?谢谢!

采纳答案by Juned Ahsan

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

JAX-WS 基于 JAXB,因此您只能将 JAXB 支持的类型作为 Web 方法参数传递。因此,任何用户定义的类正确注释如下所述,都可以用作任何 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;
    }
}

回答by John Snow

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

首先,设置您的 Web 服务调用或响应包含在 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 的字段