本文概述
- JAX-WS服务器代码
- 如何查看生成的WSDL
- JAX-WS客户端代码
你需要将Style.DOCUMENT用于@SOAPBinding注解, 而不是Style.RPC。让我们快速看一下:
文件:HelloWorld.java
@SOAPBinding(style = Style.DOCUMENT)//It is changed from RPC to DOCUMENT
现在, 让我们看一下文档样式的JAX-WS的完整示例。
【JAX-WS示例文档样式】为hello world JAX-WS文档样式示例创建了4个文件:
- HelloWorld.java
- HelloWorldImpl.java
- Publisher.java
- HelloWorldClient.java
JAX-WS服务器代码文件:HelloWorld.java
package com.srcmini;
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)
public interface HelloWorld{
@WebMethod String getHelloWorldAsString(String name);
}
文件:HelloWorldImpl.java
package com.srcmini;
import javax.jws.WebService;
//Service Implementation
@WebService(endpointInterface = "com.srcmini.HelloWorld")
public class HelloWorldImpl implements HelloWorld{
@Override
public String getHelloWorldAsString(String name) {
return "Hello World JAX-WS " + name;
}
}
文件:Publisher.java
package com.srcmini;
import javax.xml.ws.Endpoint;
//Endpoint publisher
public class HelloWorldPublisher{
public static void main(String[] args) {
Endpoint.publish("http://localhost:7779/ws/hello", new HelloWorldImpl());
}
}
如果你运行发布者类, 则可能会产生以下错误:
Wrapper class com.srcmini.GetHelloWorldAsString is not found.
Have you run APT to generate them?
要解决此问题, 请在命令提示符下转到当前项目的bin目录, 然后运行以下命令:
wsgen -keep -cp . com.srcmini.HelloWorldImpl
现在, 它将生成2个文件:
- 问好
- 说你好响应
如何查看生成的WSDL运行发布者代码后, 你可以通过访问URL来查看生成的WSDL文件:
http://localhost:7779/ws/hello?wsdl
JAX-WS客户端代码文件:HelloWorldClient.java
package com.srcmini;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
public class HelloWorldClient{
public static void main(String[] args) throws Exception {
URL url = new URL("http://localhost:7779/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://srcmini.com/", "HelloWorldImplService");
Service service = Service.create(url, qname);
HelloWorld hello = service.getPort(HelloWorld.class);
System.out.println(hello.getHelloWorldAsString("srcmini document"));
}
}
输出:
Hello World JAX-WS srcmini document
单击我下载JAX-WS服务器示例文档样式(Eclipse)
单击我下载JAX-WS客户端示例文档样式(Eclipse)
推荐阅读
- JAX-WS示例RPC样式
- RESTful JAX-RS文件上传示例
- RESTful JAX-RS文件下载示例
- RESTful JAX-RS注释示例
- JAX-RS示例jersey
- 跨平台移动端APP开发---简单高效的MUI框架
- tcp_wrapper,sudo,nsswitch与pam安全解析
- CSAPP 3e: Attack Lab
- Unity2017打包发布Android安卓整理