java中使用JDK发布WS

1、服务的发布

第一步:写一个服务接口。

import javax.jws.WebService;

@WebService
public interface HelloService {

String say(String name);
}

第二步:实现这WS接口,在实现类中完成具体业务逻辑,如下:

import javax.jws.WebService;

@WebService(
serviceName="HelloService",
portName="HelloServicePort",
endpointInterface="cn.caixw.demo.webservice.HelloService")
public class HelloServiceImpl implements HelloService {

@Override
public String say(String name) {
// TODO Auto-generated method stub
return "hello"+name;
}

}

第三步:写一个Server类,用于发布WS,直接使用JDK提供的工具即可实现。

public class WebServer {
public static void main(String[] args) {
String address="http://localhost:8080/ws/soap/hello";
HelloService helloService=new HelloServiceImpl();
Endpoint.publish(address, helloService);
System.out.println("service published");
}}

只需要使用JDK提供的javax.xml.ws.Endpoint即可发布WS,只需要提供一个WS

的地址,还需要提供一个服务实例。

第四步:打开浏览器,在地址栏中输入一下地址:

http://localhost:8080/ws/soap/hello?wsdl

在浏览器中看到如下代码:

<!--
Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<!--
Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy"xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://webservice.demo.caixw.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webservice.demo.caixw.cn/" name="HelloService">
<types>
<xsd:schema>
<xsd:import namespace="http://webservice.demo.caixw.cn/" schemaLocation="http://localhost:8080/ws/soap/hello?xsd=1"/>
</xsd:schema>
</types>
<message name="say">
<part name="parameters" element="tns:say"/>
</message>
<message name="sayResponse">
<part name="parameters" element="tns:sayResponse"/>
</message>
<portType name="HelloService">
<operation name="say">
<input wsam:Action="http://webservice.demo.caixw.cn/HelloService/sayRequest" message="tns:say"/>
<output wsam:Action="http://webservice.demo.caixw.cn/HelloService/sayResponse" message="tns:sayResponse"/>
</operation>
</portType>
<binding name="HelloServicePortBinding" type="tns:HelloService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="say">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="HelloService">
<port name="HelloServicePort" binding="tns:HelloServicePortBinding">
<soap:address location="http://localhost:8080/ws/soap/hello"/>
</port>
</service>
</definitions>
当看到这份文档的时候,意味着,发布的WSDL服务可以被别人使用了。
 

 2、通过客户端调用WS

第一步:使用JDK提供的命令行生成WS客户端jar包。

在jdk的安装目录下有个bin目录,里面存放了大量的命令行工具,其中,有一个名为wsimport的命令行工具,正是用来通过WSDL生成WS客户端代码,

只要输入如下命令行即可:

wsimport http://xxxxxxxx?wsdl   ------>通过wsdl地址生成class文件 到当前目录

jar -cf  client.jar .    ------> 将当前目录生成jar包

mdir .           -------->删除当前目录内容

第二步:写一个Client类,用于调用WS,需要使用上一步生成的WS客户端jar包。

将生成的client.jar导入到项目的classpath中,客户端代码如下:

public static void main(String[] args) {


HelloService_Service helloFactory=new HelloService_Service(); ----->类似于服务工厂。
HelloService helloService1=helloFactory.getHelloServicePort();  ----->获取服务
System.out.println(helloService1.say("bangbang"));   ------>调用服务


}

这是一个典型的“代理模式”应用场景,我们实际上是通过面向代理对象来调用WS的,并且这是一种“静态代理”,

其实JDK已经具备了动态代理的功能,对于WS而言,JDK同样也提供了很好的工具,就像下面这段代码那样:

try {
URL wsdl=new URL("http://localhost:8080/ws/soap/hello?wsdl");
QName serviceName=new QName("http://webservice.demo.caixw.cn/","HelloService");
QName portName=new QName("http://webservice.demo.caixw.cn/","HelloServicePort");

Service service=Service.create(wsdl, serviceName);
HelloService helloService=service.getPort(portName, HelloService.class);
System.out.println(helloService.say("fack"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

此时,只需要在本地提供一个HelloService的接口,无需client.jar,直接面向WSDL编程,只不过需要定义serviceName和portName这2个东西,

最后才能调用JDK提供的javax.xml.ws.Service类生成service对象,它同样是一个工厂对象,通过该工厂对象获取我们需要的HelloService实例。

貌似这样方式也不是特别动态,毕竟HelloService接口还是需要自行提供的。

原文地址:https://www.cnblogs.com/williamcai/p/6920037.html