0
点赞
收藏
分享

微信扫一扫

WebService - CXF开发Server和Client(main方法测试)

本篇简要记录使用jdk生成Stub进行service与client的测试,未与spring结合且未发布到tomcat容器,Server和Client都是使用的main方法。

篇末有第二种方式(客户端代理工厂)进行客户端开发示例。

这里使用的是CXF,引入jar永远是不可少的(自己下载引入)。

WebService - CXF开发Server和Client(main方法测试)_web service

【1】配置系统环境变量

如果使用jetty生成Stub来编写客户端项目,需要进行如下配置:

WebService - CXF开发Server和Client(main方法测试)_cxf_02

【2】编辑Service并进行发布

service的编写使用JAX-WS,直接使用注解进行声明,简单便捷。Service项目不需要额外的jar,Client项目需要博文头部提到的jar。

① 编写service接口

package com.web.service;

import javax.jws.WebMethod;
import javax.jws.WebService;

//这里使用了注解,声明为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());
}
}

WebService - CXF开发Server和Client(main方法测试)_web service_03

【3】根据address,查看并保存wsdl文件

① 浏览器中输入地址,查看wsdl

​​http://192.168.2.225:8989/ServiceServerImpl?wsdl​​

WebService - CXF开发Server和Client(main方法测试)_cxf_04

将其保存为test.wsdl放入客户端项目下(新建一个客户端项目)

WebService - CXF开发Server和Client(main方法测试)_cxf_05

<?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路径

WebService - CXF开发Server和Client(main方法测试)_cxf_06

② 根据项目下的wsdl生成Stub

wsimport -keey url

WebService - CXF开发Server和Client(main方法测试)_web service_07

③ 刷新项目,可以看到src下生成了service和impl

WebService - CXF开发Server和Client(main方法测试)_jetty_08

另外,还可以直接根据wsdl的url地址生成Stub,不用再保存wsdl具体文件到项目下。

WebService - CXF开发Server和Client(main方法测试)_jdk_09

上面两种方式都是使用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);
}
}

客户端输出结果如下:

WebService - CXF开发Server和Client(main方法测试)_cxf_10

服务端输出结果如下:

WebService - CXF开发Server和Client(main方法测试)_jetty_11

【Tips】可以采用JaxWsProxyFactoryBean 如下方式:

package com.web.test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.web.service.ServiceServer;

public class ClientTest2 {

public static void main(String[] args) {
//创建WebService客户端代理工厂
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
//注册WebService接口
factory.setServiceClass(ServiceServer.class);
//设置WebService地址
factory.setAddress("http://192.168.2.225:8888/redis/hello");
ServiceServer serverService = (ServiceServer)factory.create();
System.out.println("invoke webservice...");
String sayHello = serverService.sayHello("Lucy");
System.out.println("message context is:"+sayHello);
}
}


举报

相关推荐

0 条评论