一、简介
可以使用 cxf 和 axis2 这两种方式来实现 webService ,那么该如何选择呢?
如果应用程序需要多语言的支持,Axis2 应当是首选了。如果应用程序是遵循 Spring 哲学路线的话,Apache CXF 是一种更好的选择,特别对嵌入式的 Web Services 来说。
二、axis2 方式
2.1 客户端
因为需要对接的系统是采用的 axis2 作为服务端,我这边就采用 axis2 创建一个客户端来获取数据。
public class WebserviceClient2 {
private static long lastUpdateTime = 0;
public static void main(String[] args) {
try {
//对外发布的webService的地址
EndpointReference targetEPR = new EndpointReference("http://218.31.132.198:8092/zxris_rmt/services/configInfoService?wsdl");
RPCServiceClient sender = new RPCServiceClient();
Options options = sender.getOptions();
//超时时间20s
options.setTimeOutInMilliSeconds(2 * 20000L);
options.setTo(targetEPR);
/**
* 参数:
* 1:在网页上执行 wsdl后xs:schema标签的targetNamespace路径
* <xs:schema targetNamespace="http://axis2.com">
* 2:<xs:element name="test"> ======这个标签中name的值
*/
QName qname = new QName("http://api.WebService.business.ucms.app.rfid.zit.com", "getConfigInfo");
QueryCondition queryCondition = new QueryCondition();
queryCondition.setResultType(QueryCondition.ALL_CONFIG_INFO);
queryCondition
.setServiceId(QueryCondition.SERVICE_DETAIL_CONFIG_INFO);
queryCondition.setUpdateTime(lastUpdateTime);
String condition = JSONObject.fromObject(queryCondition).toString();
//方法的入参
Object[] param = new Object[]{condition};
//这是针对返值类型的
Class<?>[] types = new Class[]{String.class};
/**
* RPCServiceClient类的invokeBlocking方法调用了WebService中的方法。
* invokeBlocking方法有三个参数
* 第一个参数的类型是QName对象,表示要调用的方法名;
* 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
* 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
* 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}。
*/
Object[] response = sender.invokeBlocking(qname, param, types);
System.out.println(response[0]);
} catch (AxisFault e) {
e.printStackTrace();
}
}
}
三、cxf 方式
3.1 服务端
1、增加 maven 依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.2.4</version>
</dependency>
2、创建需要发布的接口和方法
@WebService
public interface VehicleInfoServer {
// 接收单条
public String writeVehicleInfo(TempVehicleInfo dVehicleInfo);
// 批量接收
public String writeVehicleInfos(List<TempVehicleInfo> list);
}
3、创建接口的实现类
@WebService
public class VehicleInfoServerImpl implements VehicleInfoServer {
public String writeVehicleInfo(TempVehicleInfo dVehicleInfo) {
try {
System.out.println("服务端被请求了一次.........................." + dVehicleInfo.toString());
return ReturnCode.SUCCESS;
} catch (Exception e) {
return e.getMessage();
}
}
public String writeVehicleInfos(List<TempVehicleInfo> list) {
for (TempVehicleInfo t : list) {
System.out.println(t.toString());
}
try {
System.out.println("服务端被请求了一次.........................." + list.size());
return ReturnCode.SUCCESS;
} catch (Exception e) {
return e.getMessage();
}
}
}
4、将接口中的方法发布出去
@Service
public class PublishWebService implements InitializingBean{
private static final Logger LOGGER = Logger.getLogger(PublishWebService.class);
@Override
public void afterPropertiesSet() throws Exception {
try {
//发布的端口号不可以和tomcat服务的端口号一致,端口号后面的内容想加什么就加什么
Endpoint.publish("http://127.0.0.1:8280/service/dataEngine/vehicleInfoService",new VehicleInfoServerImpl());
LOGGER.info(".................发布过车记录数据服务webServer成功!...................");
}catch(Exception e) {
LOGGER.error(".................发布过车记录数据webServer失败!错误信息为"+e);
}
}
3.2 客户端
public class TestWriteVehicleInfo {
public static void main(String[] args) {
String url = "http://127.0.0.1:8280/service/dataEngine/vehicleInfoService?wsdl";
//这种方式的客户端不支持axis2的server端
JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
jaxWsProxyFactoryBean.setAddress(url);
jaxWsProxyFactoryBean.setServiceClass(VehicleInfoServer.class);
VehicleInfoServer dVehicleService = (VehicleInfoServer) jaxWsProxyFactoryBean.create();
testSingleData(dVehicleService);
testListData(dVehicleService);
}
public static void testSingleData(VehicleInfoServer dVehicleService) {
TempVehicleInfo dVehicleInfo = new TempVehicleInfo();
dVehicleInfo.setHphm("津A12345");
dVehicleInfo.setHpzl("01");
dVehicleInfo.setKkbh("KK00000001");
dVehicleInfo.setSbbh("SB99999999");
dVehicleInfo.setTp1("http://isilon.whjgj.com/3334-TP1.jpg");
dVehicleInfo.setGcsj("20190514140728180");
dVehicleInfo.setTp2("http://isilon.whjgj.com/3334-TP2.jpg");
String result = dVehicleService.writeVehicleInfo(dVehicleInfo);
System.out.println(result);
}
public static void testListData(VehicleInfoServer dVehicleService) {
List<TempVehicleInfo> list = new ArrayList<>();
for(int i=0;i<5;i++) {
TempVehicleInfo dVehicleInfo = new TempVehicleInfo();
dVehicleInfo.setHphm("津A1234"+i);
dVehicleInfo.setHpzl("01");
dVehicleInfo.setKkbh("KK0000000"+i);
dVehicleInfo.setSbbh("SB99999999");
dVehicleInfo.setTp1("http://isilon.whjgj.com/3334-TP1.jpg");
dVehicleInfo.setGcsj("20190514140728180");
dVehicleInfo.setTp2("http://isilon.whjgj.com/3334-TP2.jpg");
list.add(dVehicleInfo);
}
String result = dVehicleService.writeVehicleInfos(list);
System.out.println(result);
}
}