您的位置:首页 > 编程语言 > Java开发

CFX构建webservice实例,与Spring整合

2017-07-13 09:50 471 查看
1:需要的jar包

antlr-2.7.6.jar

axis-ant.jar

axis.jar

commons-beanutils-1.8.3.jar

commons-collections-3.1.jar

commons-discovery-0.2.jar

commons-lang-2.5.jar

commons-logging-1.1.1.jar

cxf-2.6.1.jar

ezmorph-1.0.6.jar

gson-1.5.jar

jaxrpc.jar

json-lib-2.4-jdk15.jar

jxl.jar

log4j-1.2.17.jar

neethi-3.0.2.jar

ognl-3.0.6.jar

wsdl4j-1.5.1.jar

xmlschema-core-2.0.2.jar

jetty-continuation-8.1.15.v20140411.jar

jetty-http-8.1.15.v20140411.jar

jetty-io-8.1.15.v20140411.jar

jetty-security-8.1.15.v20140411.jar

jetty-server-8.1.15.v20140411.jar

jetty-util-8.1.15.v20140411.jar

 

2:配置web.xml

<!-- webservice接口 -->
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>


 

3:定义接口

 

package com.vrv.ccp.service;

import javax.jws.WebService;

/**
*     <B>说明</B>:
*
*     @author  作者  名:段文杰<br/>
*     @version版  本  号:V1.0.12.0<br/>
*             创建时间:   2017年7月11日上午9:26:11
*     @email   duanwenjie@vrvmail.com.cn
*
*/
@WebService
public interfaceDeviceInfoAllService {

String sayHiToUser(String str);

/**
* 查询所有设备信息
*/
String queryAllDeviceInfo();

/**
* 查询存在超密文件数
*/
String queryFileIllegalCount();
}


4:定义实现类

 

package com.vrv.ccp.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jws.WebService;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;

import com.vrv.ccp.domain.Device;
import com.vrv.ccp.domain.DeviceInfoAllBean;
import com.vrv.ccp.domain.DeviceUser;
import com.vrv.ccp.service.CheckReportService;
import com.vrv.ccp.service.DeviceInfoAllService;
import com.vrv.ccp.service.DeviceService;
import com.vrv.ccp.service.DeviceUserService;

/**
*     <B>说明</B>:
*
*     @author  作者  名:段文杰<br/>
*     @version版  本  号:V1.0.12.0<br/>
*             创建时间:   2017年7月11日上午9:27:56
*     @email   duanwenjie@vrvmail.com.cn
*
*/
@WebService(endpointInterface="com.vrv.ccp.service.DeviceInfoAllService",serviceName="DeviceInfoAllService",targetNamespace="http://service.ccp.vrv.com/")
public classDeviceInfoAllServiceImpl implements DeviceInfoAllService {

// publicApplicationContext applicationContext;
//
// publicDeviceInfoAllServiceImpl(ApplicationContext applicationContext){
//    this.applicationContext= applicationContext;
// }
@Resource
private DeviceUserService deviceUserService;
@Resource
private DeviceService deviceService;
@Resource
private CheckReportService checkReportService;

private Logger logger = Logger.getLogger(DeviceInfoAllServiceImpl.class);
@Override
public StringsayHiToUser(String str) {
// TODO Auto-generated method stub
logger.info("=========DeviceInfoAllService接口=========="+str);
return "DeviceInfoAllService接口";
}

@Override
public StringqueryAllDeviceInfo() {

// 查询所有台账并封装map集合
Map<Long,DeviceUser> deviceUserMap = new HashMap<Long, DeviceUser>();
//    DeviceUserServicedeviceUserService = (DeviceUserService)applicationContext.getBean(DeviceUserService.class);
List<DeviceUser>deviceUserList = deviceUserService.queryAll();
for (DeviceUser du :deviceUserList) {
if(du.getDevice()!=null){
deviceUserMap.put(du.getDevice().getId(),du);
}
}
//查询所有的设备信息
//    DeviceServicedeviceService = (DeviceService)applicationContext.getBean(DeviceService.class);
List<Device>deviceList = deviceService.queryAll();
List<DeviceInfoAllBean>deviceInfoAllBeanList = new ArrayList<DeviceInfoAllBean>();
SimpleDateFormatdf = newSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//遍历并封装
for(Devicedevice:deviceList){
DeviceInfoAllBeandeviceInfoAllBean = new DeviceInfoAllBean();
DeviceUserdeviceUser = deviceUserMap.get(device.getId());
deviceInfoAllBean.setIp(device.getIp());
if (null != deviceUser) {
deviceInfoAllBean.setDeviceUser(deviceUser.getName());
}
deviceInfoAllBean.setOrgName(device.getCurOrgName());
deviceInfoAllBean.setSecretDegree(device.getSecretDegree());
deviceInfoAllBean.setClientVersion(device.getClientVersion());
deviceInfoAllBean.setOnlineActive(device.getOnlineActive());
deviceInfoAllBean.setoTime(device.getoTime());
deviceInfoAllBean.setRegisterTime(df.format(device.getRegisterTime()));
deviceInfoAllBeanList.add(deviceInfoAllBean);
}
JSONArrayjsonarray = JSONArray.fromObject(deviceInfoAllBeanList);
return jsonarray.toString();
}

@Override
public StringqueryFileIllegalCount() {
// TODO Auto-generated method stub
//查询违规文件个数
//    CheckReportServicecheckReportService = (CheckReportService)applicationContext.getBean(CheckReportService.class);
int count = checkReportService.queryIllegalCount();
Stringjson = "[{'count':'"+count+"'}]";
return json;
}

}


5:配置applicationContext-webservice.xml

<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd">
<!--
手动添加的内容:
xmlns:jaxws="http://cxf.apache.org/jaxws" http://cxf.apache.org/jaxwshttp://cxf.apache.org/schemas/jaxws.xsd" -->

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<jaxws:endpoint id="helloWorld" implementor="com.vrv.ccp.service.impl.HelloWorldImpl"address="/helloWorld" />

<jaxws:endpoint id="deviceInfoAllServiceName" implementor="com.vrv.ccp.service.impl.DeviceInfoAllServiceImpl"address="/deviceInfoAllService" />
</beans>


6:重启并访问
http://192.168.118.130:8088/CCP/webservice/deviceInfoAllService?wsdl
 

7:客户端调用webservice2种方式;

 

http请求:

package client;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.cxf.endpoint.Client;
importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
*    <B>说明 </B>:
*
*    @author  作 者  名 :段文杰<br/>
*    @version 版  本  号 :V1.0.12.0<br/>
*             创建时间:   2017年7月11日 上午9:48:47
*    @email   duanwenjie@vrvmail.com.cn
*
*/

public class DeviceInfoAllServiceClient {

publicstatic void main(String[] args) {
dy2();
//      dy3();
dy4();
}

publicstatic void dyServer(){

}

publicstatic void dy2() {
try{
Gsongson = new Gson();

StringtdsUrl  ="http://192.168.118.130:8088/CCP/webservice/deviceInfoAllService?wsdl";
System.out.println("=========tdsUrl=========="+tdsUrl);
JaxWsDynamicClientFactorydcf = JaxWsDynamicClientFactory.newInstance();
System.out.println("=========dcf=========="+dcf);
Stringmethod = "queryAllDeviceInfo";
Clientclient = dcf.createClient(tdsUrl);
System.out.println("=========client=========="+client);
Object[]res = null;
res= client.invoke(method, "");
if(res != null) {
System.out.println("=========res.length========["+res.length+"]");
Stringresult = res[0].toString();
System.out.println("=========result========"+result);
List<DeviceInfoAllBean>deviceInfoAllBeanList = gson.fromJson(result,
newTypeToken<List<DeviceInfoAllBean>>() {
}.getType());
System.out.println("=========list条数:========["+deviceInfoAllBeanList.size()+"]");
for(DeviceInfoAllBeandeviceInfoAllBean:deviceInfoAllBeanList){
System.out.println(deviceInfoAllBean.getIp());
}
}
}catch (Exception e) {
e.printStackTrace();
}
}

publicstatic void dy4() {
try{
StringtdsUrl  ="http://192.168.118.130:8088/CCP/webservice/deviceInfoAllService?wsdl";
System.out.println("=========tdsUrl=========="+tdsUrl);
JaxWsDynamicClientFactorydcf = JaxWsDynamicClientFactory.newInstance();
System.out.println("=========dcf=========="+dcf);
Stringmethod = "queryFileIllegalCount";
Clientclient = dcf.createClient(tdsUrl);
System.out.println("=========client=========="+client);
Object[]res = null;
res= client.invoke(method, "");
if(res != null) {
System.out.println("=========res.length========["+res.length+"]");
Stringresult = res[0].toString();
System.out.println("=========result========"+result);
JSONArrayjson = JSONArray.fromObject(result);
if(json.size()>0){
for(int i=0;i<json.size();i++){
JSONObject job =json.getJSONObject(i);  // 遍历 jsonarray 数组,把每一个对象转成json 对象
System.out.println(job.get("count"));  // 得到 每个对象中的属性值
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
}

publicstatic void dy3(){
StringtdsUrl="http://192.168.119.174/ntds/WebServer/AssetManagement.asmx?wsdl";
try{
JaxWsDynamicClientFactorydcf = JaxWsDynamicClientFactory
.newInstance();
org.apache.cxf.endpoint.Clientclient = dcf
.createClient(tdsUrl);

intTotalCount = 0;
intpageSize = 1000;
inti = 1;
while(true){
//如果IsPagination为true则解析
if((i-1)*pageSize>TotalCount){
break;
}
//sayHello 为接口中定义的方法名称张三为传递的参数返回一个Object数组
//查询已注册数据
Stringparam = "{'SearchCondition':{'Registered':1},'OrderField':'d.DeviceID','SortType':0,'PageSize':"+pageSize+",'PageIndex':"+i+",'TotalCount':0,'TotalPageCount':0,'IsPagination':true}";
Stringmethod = "GetDeviceAll";
Object[]objects = client.invoke(method, param);
//输出调用结果
System.out.println("==============objects[0].toString()=========["+objects[0].toString()+"]");
JSONObjectjson = JSONObject.fromObject(objects[0].toString());
//解析json 获取数据并入库操作
//                                  TotalCount= parseJson(json);
i++;
System.out.println("==============i=========["+i+"]");
}

}catch (Exception e) {
e.printStackTrace();
}
}
}


https请求:

 

package client;
/**
*    <B>说明 </B>:
*
*    @author  作 者  名 :段文杰<br/>
*    @version 版  本  号 :V1.0.12.0<br/>
*             创建时间:   2017年7月12日 下午6:22:58
*    @email   duanwenjie@vrvmail.com.cn
*
*/

import javax.xml.namespace.QName;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class TestEcVoteNotice{

public static void main(String [] args) throws Exception {
System.setProperty("javax.net.ssl.keyStore", "D:\\tomcat证书\\tomcat.keystore");
System.setProperty("javax.net.ssl.keyStorePassword","123456789");
System.setProperty("javax.net.ssl.trustStore","D:\\tomcat证书\\tomcat.truststore");
System.setProperty("javax.net.ssl.trustStorePassword","123456789");

String endpoint ="https://192.168.118.130:8088/CCP/webservice/deviceInfoAllService?wsdl";
Service service = new Service();
Call call = (Call)service.createCall();
call.setTargetEndpointAddress( new java.net.URL(endpoint) );
call.setOperationName(new QName("http://service.ccp.vrv.com/","sayHiToUser"));
String res = (String) call.invoke( new Object[] {} );
call.setOperationName(new QName("http://service.ccp.vrv.com/","queryFileIllegalCount"));
String res2 = (String) call.invoke( new Object[] {} );

System.out.println( res+"/"+res2 );
}
}


8:https请求证书生成

 

使用jdk生成keystore后,导出证书,然后生成truststore文件

前面网上例子很多,就不贴代码

 

生成truststore文件:

keytool -import -file D:\tomcat证书\server.cer-storepass 123456789 -keystore D:\tomcat证书\tomcat.truststore-alias tomcat -noprompt

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java web service jar spring