JAX-WS与JDK 1.6捆绑在一起,这使得Java Web服务开发更易于开发。 本教程向您展示如何执行以下任务:
- 使用JAX-WS创建基于SOAP的RPC样式的Web服务终结点。
- 手动创建Java Web服务客户端。
- 通过wsimport工具创建Java Web服务客户端。
- 创建一个Ruby Web服务客户端。
您将惊讶于用JAX-WS开发RPC样式的Web服务如此简单。
注意
概括而言,“ Web服务端点 ”是在外部发布以供用户访问的服务; 其中“ 网络服务客户端 ”是访问已发布服务的一方。
JAX-WS Web服务端点
以下步骤显示了如何使用JAX-WS创建RPC样式的Web服务端点。
1.创建一个Web服务端点接口
文件:HelloWorld.java
package com.mkyong.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
//Service Endpoint Interface
@WebService
@SOAPBinding(style = Style.RPC)
public interface HelloWorld{
@WebMethod String getHelloWorldAsString(String name);
}
2.创建一个Web服务端点实现
文件:HelloWorldImpl.java
package com.mkyong.ws;
import javax.jws.WebService;
//Service Implementation
@WebService(endpointInterface = "com.mkyong.ws.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
3.创建一个端点发布者
文件:HelloWorldPublisher.java
package com.mkyong.endpoint;
import javax.xml.ws.Endpoint;
import com.mkyong.ws.HelloWorldImpl;
//Endpoint publisher
public class HelloWorldPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:9999/ws/hello", new HelloWorldImpl());
}
}
运行端点发布者,您的“ hello world web service ”将部署在URL“ http:// localhost:9999 / ws / hello ”中。
4.测试
您可以通过以下URL“ http:// localhost:9999 / ws / hello?wsdl ”访问生成的WSDL(Web服务定义语言)文档来测试已部署的Web服务。
Web服务客户端
好的,Web服务已正确部署,现在让我们看看如何创建Web服务客户端以访问发布的服务。
1. Java Web服务客户端
没有工具,您可以像这样创建Java Web服务客户端:
package com.mkyong.client;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import com.mkyong.ws.HelloWorld;
public class HelloWorldClient{
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:9999/ws/hello?wsdl");
//1st argument service URI, refer to wsdl document above
//2nd argument is service name, refer to wsdl document above
QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.getHelloWorldAsString("mkyong"));
}
}
输出量
Hello World JAX-WS mkyong
2.通过wsimport工具的Java Web Service客户端
或者,您可以使用“ wsimport ”工具解析已发布的wsdl文件,并生成必要的客户端文件(存根)以访问已发布的Web服务。
wsimport在哪里?
该wsimport工具与JDK捆绑在一起,您可以在“ JDK_PATH / bin ”文件夹中找到它。
发出“ wsimport ”命令。
wsimport -keep http://localhost:9999/ws/hello?wsdl
它将生成必要的客户端文件,该文件取决于所提供的wsdl文件。 在这种情况下,它将生成一个接口和一个服务实现文件。
文件:HelloWorld.java
package com.mkyong.ws;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.1 in JDK 6
* Generated source version: 2.1
*
*/
@WebService(name = "HelloWorld", targetNamespace = "http://ws.mkyong.com/")
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface HelloWorld {
/**
*
* @param arg0
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(partName = "return")
public String getHelloWorldAsString(
@WebParam(name = "arg0", partName = "arg0")
String arg0);
}
文件:HelloWorldImplService.java
package com.mkyong.ws;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.1.1 in JDK 6
* Generated source version: 2.1
*
*/
@WebServiceClient(name = "HelloWorldImplService",
targetNamespace = "http://ws.mkyong.com/",
wsdlLocation = "http://localhost:9999/ws/hello?wsdl")
public class HelloWorldImplService
extends Service
{
private final static URL HELLOWORLDIMPLSERVICE_WSDL_LOCATION;
static {
URL url = null;
try {
url = new URL("http://localhost:9999/ws/hello?wsdl");
} catch (MalformedURLException e) {
e.printStackTrace();
}
HELLOWORLDIMPLSERVICE_WSDL_LOCATION = url;
}
public HelloWorldImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public HelloWorldImplService() {
super(HELLOWORLDIMPLSERVICE_WSDL_LOCATION,
new QName("http://ws.mkyong.com/", "HelloWorldImplService"));
}
/**
*
* @return
* returns HelloWorld
*/
@WebEndpoint(name = "HelloWorldImplPort")
public HelloWorld getHelloWorldImplPort() {
return (HelloWorld)super.getPort(
new QName("http://ws.mkyong.com/", "HelloWorldImplPort"),
HelloWorld.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy.
* Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns HelloWorld
*/
@WebEndpoint(name = "HelloWorldImplPort")
public HelloWorld getHelloWorldImplPort(WebServiceFeature... features) {
return (HelloWorld)super.getPort(
new QName("http://ws.mkyong.com/", "HelloWorldImplPort"),
HelloWorld.class,
features);
}
}
现在,创建一个依赖于上面生成的文件的Java Web服务客户端。
package com.mkyong.client;
import com.mkyong.ws.HelloWorld;
import com.mkyong.ws.HelloWorldImplService;
public class HelloWorldClient{
public static void main(String[] args) {
HelloWorldImplService helloService = new HelloWorldImplService();
HelloWorld hello = helloService.getHelloWorldImplPort();
System.out.println(hello.getHelloWorldAsString("mkyong"));
}
}
这是输出
Hello World JAX-WS mkyong
3. Ruby Web服务客户端
通常,Web服务开发与其他编程语言混合使用。 因此,这是一个Ruby Web服务客户端示例,用于访问已发布的JAX-WS服务。
# package for SOAP-based services
require 'soap/wsdlDriver'
wsdl_url = 'http://localhost:9999/ws/hello?wsdl'
service = SOAP::WSDLDriverFactory.new(wsdl_url).create_rpc_driver
# Invoke service operations.
data1 = service.getHelloWorldAsString('mkyong')
# Output results.
puts "getHelloWorldAsString : #{data1}"
输出量
getHelloWorldAsString : Hello World JAX-WS mkyong
跟踪SOAP流量
从上到下,显示SOAP信封如何在客户端和服务器之间流动。 再次参见#1 Web服务客户端:
URL url = new URL("http://localhost:9999/ws/hello?wsdl");
QName qname = new QName("http://ws.mkyong.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.getHelloWorldAsString("mkyong"));
注意
要监视SOAP通信非常容易,请参阅本指南-“ 如何在Eclipse IDE中跟踪SOAP消息 ”。
1.请求WSDL文件
首先,客户端将wsdl请求发送到服务端点,请参见下面的HTTP流量:
客户发送请求:
GET /ws/hello?wsdl HTTP/1.1
User-Agent: Java/1.6.0_13
Host: localhost:9999
Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Connection: keep-alive
服务器发送响应:
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml;charset=utf-8
<?xml version="1.0" encoding="UTF-8"?>
<definitions
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://ws.mkyong.com/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://ws.mkyong.com/"
name="HelloWorldImplService">
<types></types>
<message name="getHelloWorldAsString">
<part name="arg0" type="xsd:string"></part>
</message>
<message name="getHelloWorldAsStringResponse">
<part name="return" type="xsd:string"></part>
</message>
<portType name="HelloWorld">
<operation name="getHelloWorldAsString" parameterOrder="arg0">
<input message="tns:getHelloWorldAsString"></input>
<output message="tns:getHelloWorldAsStringResponse"></output>
</operation>
</portType>
<binding name="HelloWorldImplPortBinding" type="tns:HelloWorld">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"></soap:binding>
<operation name="getHelloWorldAsString">
<soap:operation soapAction=""></soap:operation>
<input>
<soap:body use="literal" namespace="http://ws.mkyong.com/"></soap:body>
</input>
<output>
<soap:body use="literal" namespace="http://ws.mkyong.com/"></soap:body>
</output>
</operation>
</binding>
<service name="HelloWorldImplService">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:9999/ws/hello"></soap:address>
</port>
</service>
</definitions>
2. hello.getHelloWorldAsString()
第二个调用,即客户端放置方法调用SOAP信封中的请求,并将其发送到服务端点。 在服务端点,调用请求的方法,并将结果放入SOAP信封中,然后将其发送回客户端。
客户发送请求:
POST /ws/hello HTTP/1.1
SOAPAction: ""
Accept: text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
Content-Type: text/xml; charset=utf-8
User-Agent: Java/1.6.0_13
Host: localhost:9999
Connection: keep-alive
Content-Length: 224
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getHelloWorldAsString xmlns:ns2="http://ws.mkyong.com/">
<arg0>mkyong</arg0>
</ns2:getHelloWorldAsString>
</S:Body>
</S:Envelope>
服务器发送响应:
HTTP/1.1 200 OK
Transfer-encoding: chunked
Content-type: text/xml; charset=utf-8
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:getHelloWorldAsStringResponse xmlns:ns2="http://ws.mkyong.com/">
<return>Hello World JAX-WS mkyong</return>
</ns2:getHelloWorldAsStringResponse>
</S:Body>
</S:Envelope>
做完,任何意见表示赞赏。
下载源代码
下载它– JAX-WS-HelloWorld-RPC-Example.zip (14KB)
翻译自: https://mkyong.com/webservices/jax-ws/jax-ws-hello-world-example/