本篇简要记录使用jdk生成Stub进行service与client的测试,未与spring结合。这里使用的是CXF。 【1】配置系统环境变量 如果使用jetty生成Stub来编写客户端项目,需要进行如下配置: 【2】编辑
本篇简要记录使用jdk生成Stub进行service与client的测试,未与spring结合。这里使用的是CXF。
【1】配置系统环境变量
如果使用jetty生成Stub来编写客户端项目,需要进行如下配置:
【2】编辑Service并进行发布
① 编写service接口
package com.web.service;
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public interface ServiceServer {
@WebMethod
public String sayHello(String name);
}
② 编写其实现类
package com.web.service.impl;
import javax.jws.WebService;
import com.web.service.ServiceServer;
@WebService
public class ServiceServerImpl implements ServiceServer{
@Override
public String sayHello(String name) {
System.out.println("server sayHello "+name);
return "hello"+name;
}
}
③ 使用main方法进行发布
package com.web.service.impl;
import java.util.Date;
import javax.xml.ws.Endpoint;
public class ServerTest {
public static void main(String[] args) {
String address = "http://192.168.2.225:8989/ServiceServerImpl";
Endpoint.publish(address , new ServiceServerImpl());
System.out.println("webservice 发布成功!"+new Date());
}
}
【3】根据address,查看并保存wsdl文件
① 浏览器中输入地址,查看wsdl
http://192.168.2.225:8989/ServiceServerImpl?wsdl
将其保存为test.wsdl放入客户端项目下(新建一个客户端项目)
<?xml version='1.0' encoding='UTF-8'?><wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://impl.service.web.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://service.web.com/" name="ServiceServerImplService" targetNamespace="http://impl.service.web.com/">
<wsdl:import location="http://192.168.2.225:8989/ServiceServerImpl?wsdl=ServiceServer.wsdl" namespace="http://service.web.com/">
</wsdl:import>
<wsdl:binding name="ServiceServerImplServiceSoapBinding" type="ns1:ServiceServer">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="ServiceServerImplService">
<wsdl:port binding="tns:ServiceServerImplServiceSoapBinding" name="ServiceServerImplPort">
<soap:address location="http://192.168.2.225:8989/ServiceServerImpl"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
【4】根据wsdl生成Stub,进行客户端测试
① DOS进入客户端项目src路径
② 根据项目下的wsdl生成Stub
wsimport -keey url
③ 刷新项目,可以看到src下生成了service和impl
另外,还可以直接根据wsdl的url地址生成Stub,不用再保存wsdl具体文件到项目下。
上面两种方式都是使用jdk生成Stub,还可以使用如下命令使用jetty进行生成:
wsdl2java url
④ 编写main方法,进行客户端调用
package com.web.client;
import com.web.service.impl.ServiceServer;
import com.web.service.impl.ServiceServerImplService;
public class Client {
public static void main(String[] args) {
ServiceServerImplService factory = new ServiceServerImplService();
ServiceServer serviceServerImplPort = factory.getServiceServerImplPort();
System.out.println(serviceServerImplPort.getClass());
String result = serviceServerImplPort.sayHello("Tom");
System.out.println("Client "+result);
}
}
客户端输出结果如下:
服务端输出结果如下:
项目源码与jar下载:http://download.csdn.net/download/j080624/10051042