本文概述
创建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;
}
}
文件: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://srcmini02.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)
评论前必须登录!
注册