WebService Jax-ws 简单应用
目前需要用到jax-ws,所以在进行学习,当前给出一个jax-ws的应用流程:
1.创建接口HelloWorldService接口及方法
/**
* @(#) IHelloService.java
*/
package com.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
/**
* @version 1.0
* @Function 类功能说明:创建服务端接口
*/
@WebService
@SOAPBinding(style = Style.DOCUMENT)
public interface IHelloWorldService {
@WebMethod
public String sayHelloWorld(@WebParam(name= "name")String name);
}
2.创建接口的实现类,并为接口注入@WebService
/**
* @(#) HelloWorldImpl.java
*/
package com.webservice;
import javax.jws.WebService;
/**
* @version 1.0
* @Function 类功能说明:实现接口,并注入webservice注解
*/
@WebService(endpointInterface = "com.webservice.IHelloWorldService")
public class HelloWorldServiceImpl implements IHelloWorldService{
public String sayHelloWorld(String name) {
return "Hello World : "+name;
}
}
3.编写发布类,通过Endpoint将接口发布出去
/**
* @(#) WsPublisher.java
*/
package com.webservice;
import javax.xml.ws.Endpoint;
/**
* @version 1.0
* @Function 类功能说明:发布服务端的接口
*/
public class WsPublisher {
public static void main(String[] args) {
System.out.println("开始发布。。。");
Endpoint.publish("http://127.0.0.1:8012/hello_world", new HelloWorldServiceImpl());
System.out.println("发布成功。。。");
}
}
运行WsPublisher后,有以下的效果:
开始发布。。。
2013-8-31 9:24:30 com.sun.xml.internal.ws.model.RuntimeModeler getRequestWrapperClass
信息: Dynamically creating request wrapper Class com.webservice.jaxws.SayHelloWorld
2013-8-31 9:24:30 com.sun.xml.internal.ws.model.RuntimeModeler getResponseWrapperClass
信息: Dynamically creating response wrapper bean Class com.webservice.jaxws.SayHelloWorldResponse
发布成功。。。
这时候就可打开浏览器查看http://127.0.0.1:8012/hello_world?wsdl 有如下效果说明发布成功了
<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6.
-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.com/" name="HelloWorldServiceImplService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.com/" schemaLocation="http://127.0.0.1:8012/hello_world?xsd=1"/>
</xsd:schema>
</types>
<message name="sayHelloWorld">
<part name="parameters" element="tns:sayHelloWorld"/>
</message>
<message name="sayHelloWorldResponse">
<part name="parameters" element="tns:sayHelloWorldResponse"/>
</message>
<portType name="IHelloWorldService">
<operation name="sayHelloWorld">
<input message="tns:sayHelloWorld"/>
<output message="tns:sayHelloWorldResponse"/>
</operation>
</portType>
<binding name="HelloWorldServiceImplPortBinding" type="tns:IHelloWorldService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="sayHelloWorld">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloWorldServiceImplService">
<port name="HelloWorldServiceImplPort" binding="tns:HelloWorldServiceImplPortBinding">
<soap:address location="http://127.0.0.1:8012/hello_world"/>
</port>
</service>
</definitions>
4.创建相关的文件夹,在cmd命令行下,使用wsgen指令,生成相应的wsdl文件和异常处理的相关类 wsgen -cp .;bin/ -r ws/wsdl -s ws/src -d ws/bin -wsdl com.webservice.HelloWorldServiceImpl (没办法上传图片确实比较坑)
5.创建相关的文件夹,使用wsimport指令生成客户端代码
wsimport -s client/src -d client/bin -p com.webservice.client http://localhost:8012/hello_world?wsdl
生成相应的接口:
com.webservice.client.HelloWorldServiceImplService
com.webservice.client.IHelloWorldService
com.webservice.client.ObjectFactory
com.webservice.client.package-info
com.webservice.client.SayHelloWorld
com.webservice.client.SayHelloWorldResponse
6.最后就可以写客户端代码,进行调用了。
/**
* @(#) HelloWorldClient.java
*/
package com.webservice.client.test;
import com.webservice.client.HelloWorldServiceImplService;
import com.webservice.client.IHelloWorldService;
/**
* @version 1.0
* @Function 类功能说明:客户端调用
*/
public class HelloWorldClient {
public static void main(String[] args) {
HelloWorldServiceImplService service = new HelloWorldServiceImplService();
IHelloWorldService worldService = service.getHelloWorldServiceImplPort();
System.out.println(worldService.sayHelloWorld("hahaha"));
}
}
运行客户端输出结果:
Hello World : hahaha
到这里整个流程也就跑完了,现在是刚开始学习webservice的jax-ws;以后还需要继续深入学习,内容会有一定的更新