我实现了一个简单的 Web 服务,它有一个方法,该方法接受一个字符串并返回一条包含输入参数的消息。
package com.product.mobile.webapp.soap;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
@WebService
@SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL, parameterStyle = ParameterStyle.WRAPPED)
public class WSHello {
@WebMethod
public String sayMyName(@WebParam(name = "name", mode = Mode.IN) String name) {
return "Hello, ... " + name;
}
}
我将这样发布此端点:
WSHello wsHello = new WSHello();
String wsHelloEndpoint = "http://localhost:8080/hello";
Endpoint.publish(wsHelloEndpoint, wsHello);
当我启动应用程序时,将创建以下 WSDL,并可在 http://localhost:8080/hello?wsdl
<?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --><definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://soap.webapp.mobile.product.at/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://soap.webapp.mobile.product.at/" name="WSHelloService">
<types>
<xsd:schema>
<xsd:import namespace="http://soap.webapp.mobile.product.at/" schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="sayMyName">
<part name="parameters" element="tns:sayMyName"></part>
</message>
<message name="sayMyNameResponse">
<part name="parameters" element="tns:sayMyNameResponse"></part>
</message>
<portType name="WSHello">
<operation name="sayMyName">
<input wsam:Action="http://soap.webapp.mobile.product.at/WSHello/sayMyNameRequest" message="tns:sayMyName"></input>
<output wsam:Action="http://soap.webapp.mobile.product.at/WSHello/sayMyNameResponse" message="tns:sayMyNameResponse"></output>
</operation>
</portType>
<binding name="WSHelloPortBinding" type="tns:WSHello">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding>
<operation name="sayMyName">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal"></soap:body>
</input>
<output>
<soap:body use="literal"></soap:body>
</output>
</operation>
</binding>
<service name="WSHelloService">
<port name="WSHelloPort" binding="tns:WSHelloPortBinding">
<soap:address location="http://localhost:8080/hello"></soap:address>
</port>
</service>
</definitions>
在客户端,我可以访问我的网络服务,还可以访问方法 sayMyName(String name)
我的问题是我无法向客户端的函数提供名称参数。我假设生成的 WSDL 是错误的,因为它不包含参数信息。
有人可以向我解释一下我在这里做错了什么吗?为什么 WSDL 不包含参数参数?
最佳答案
您所说的参数是在“schemaLocation”属性中指定的。 见下文。
<xsd:import namespace="http://soap.webapp.mobile.product.at/" schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import>
所以可以查看“http://localhost:8080/hello?xsd=1”中的参数信息。
如果您想从 ?wsdl 生成 java 客户端,
wsimport -keep http://localhost:8080/hello?wsdl
你知道“-keep”意味着“用源代码生成”
您可以获得一组客户端代码。
我有另一个建议,
如果您想将 XSD 包含到 WSDL 中以使“birt”等工具理解 WSDL,
尝试使用内联模式(没有 schemaLocation)生成 WSDL,如下所示,
wsgen -cp . com.product.mobile.webapp.soap.WSHello -wsdl -inlineSchemas
将创建“WSHelloService.wsdl”文件。
在 WSHello 类中编辑 @WebService 注释,如下所示。
@WebService(wsdlLocation="WSHelloService.wsdl")
@SOAPBinding(style = Style.DOCUMENT, use = Use.LITERAL, parameterStyle = ParameterStyle.WRAPPED)
public class WSHello {
@WebMethod
public String sayMyName(@WebParam(name = "name", mode = Mode.IN) String name) {
return "Hello, ... " + name;
}
}
重新启动服务器并使用您的工具访问“http://localhost:8080/hello?wsdl ”。
希望有帮助。
关于java - 使用默认 Java 1.6 java.xml.ws API 生成的 WSDL 中缺少 SOAP 方法参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39768971/