在本教程中,我们向您展示如何使用JAX-WS创建基于SOAP的Web服务(文档样式)端点。 与RPC样式相比 ,它需要付出一些额外的努力才能使其正常工作。
本示例的目录结构
JAX-WS Web服务端点
以下是在JAX-WS中创建文档样式Web服务的步骤。
1.创建一个Web服务端点接口
实际上,用@SOAPBinding
注释是可选的,因为默认样式是document。
文件: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.DOCUMENT, use=Use.LITERAL) //optional
public interface HelloWorld{
@WebMethod String getHelloWorldAsString(String name);
}
注意
在JAX-WS开发中,从“ RPC样式 ”转换为“ 文档样式 ”非常容易,只需更改@SOAPBinding
样式选项即可。
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.创建一个Endpoint Publisher。
文件: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());
}
}
等待,当您运行端点发布者时,您将遇到以下错误消息:
Wrapper class com.mkyong.ws.jaxws.GetHelloWorldAsString is not found.
Have you run APT to generate them?
看到这篇文章 。 您需要使用“ wsgen ”工具来生成必要的JAX-WS可移植工件。 让我们继续下一步。
4. wsgen命令
文档样式需要运行额外的类,您可以使用“ wsgen ”生成所有必要的Java构件(映射类,wsdl或xsd模式)。 需要“ wsgen ”命令来读取服务端点实现类:
wsgen -keep -cp . com.mkyong.ws.HelloWorldImpl
它将生成两个类,并将其复制到您的“ package.jaxws ”文件夹中。
文件:GetHelloWorldAsString.java
package com.mkyong.ws.jaxws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "getHelloWorldAsString", namespace = "http://ws.mkyong.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getHelloWorldAsString", namespace = "http://ws.mkyong.com/")
public class GetHelloWorldAsString {
@XmlElement(name = "arg0", namespace = "")
private String arg0;
/**
*
* @return
* returns String
*/
public String getArg0() {
return this.arg0;
}
/**
*
* @param arg0
* the value for the arg0 property
*/
public void setArg0(String arg0) {
this.arg0 = arg0;
}
}
文件:GetHelloWorldAsStringResponse.java
package com.mkyong.ws.jaxws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlRootElement(name = "getHelloWorldAsStringResponse", namespace = "http://ws.mkyong.com/")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "getHelloWorldAsStringResponse", namespace = "http://ws.mkyong.com/")
public class GetHelloWorldAsStringResponse {
@XmlElement(name = "return", namespace = "")
private String _return;
/**
*
* @return
* returns String
*/
public String getReturn() {
return this._return;
}
/**
*
* @param _return
* the value for the _return property
*/
public void setReturn(String _return) {
this._return = _return;
}
}
注意
“ wsgen”工具位于“ JDK_Path \ bin \”文件夹中。 有关详细信息,请阅读此JAX-WS:wsgen工具示例文章。
5.完成
完成,发布并通过URL测试: http:// localhost:9999 / ws / hello?wsdl 。
Web服务客户端
创建一个Web服务客户端以访问您的已发布服务。
文件:HelloWorldClient.java
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");
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
跟踪SOAP流量
从上到下,显示了此文档样式的Web服务中SOAP信封如何在客户端和服务器之间流动。
1.请求WSDL文件
首先,客户端向服务端点发送wsdl请求:
客户发送请求:
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"?>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net.
RI's version is JAX-WS RI 2.1.1 in JDK 6. -->
<!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net.
RI's version is JAX-WS RI 2.1.1 in JDK 6. -->
<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>
<xsd:schema>
<xsd:import namespace="http://ws.mkyong.com/"
schemaLocation="http://localhost:9999/ws/hello?xsd=1"></xsd:import>
</xsd:schema>
</types>
<message name="getHelloWorldAsString">
<part name="parameters" element="tns:getHelloWorldAsString"></part>
</message>
<message name="getHelloWorldAsStringResponse">
<part name="parameters" element="tns:getHelloWorldAsStringResponse"></part>
</message>
<portType name="HelloWorld">
<operation name="getHelloWorldAsString">
<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="document">
</soap:binding>
<operation name="getHelloWorldAsString">
<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="HelloWorldImplService">
<port name="HelloWorldImplPort" binding="tns:HelloWorldImplPortBinding">
<soap:address location="http://localhost:9999/ws/hello"></soap:address>
</port>
</service>
</definitions>
2. 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-Document-Example.zip (10KB)
翻译自: https://mkyong.com/webservices/jax-ws/jax-ws-hello-world-example-document-style/