本文概述
- JAX-WS服务器代码
- 如何查看生成的WSDL
- JAX-WS客户端代码
JAX-WS API内置在JDK中, 因此你不需要为其加载任何额外的jar文件。让我们看一个RPC样式的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.RPC)
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;
}
}
【JAX-WS示例RPC样式】文件: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());
}
}
如何查看生成的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 rpc"));
}
}
输出:
Hello World JAX-WS srcmini rpc
单击我下载JAX-WS服务器示例RPC样式(Eclipse)
单击我下载JAX-WS客户端示例RPC样式(Eclipse)
推荐阅读
- RESTful Web服务
- JAX-WS示例文档样式
- RESTful JAX-RS文件上传示例
- RESTful JAX-RS文件下载示例
- RESTful JAX-RS注释示例
- JAX-RS示例jersey
- 跨平台移动端APP开发---简单高效的MUI框架
- tcp_wrapper,sudo,nsswitch与pam安全解析
- CSAPP 3e: Attack Lab