基于spring使用cxf框架调用webservice【方法二】
(1)使用cmd命令行生成源码文件:
我的webservice的url链接是:http://197.3.176.227:15060/queryPerCusDetailNew?wsdl
进入一个文件目录,在cmd窗口输入:
wsdl2java http://197.3.176.227:15060/queryPerCusDetailNew?wsdl
即可在当前目录获取到源码文件
(2)将源码文件复制粘贴到javaweb项目中
(3)在pom文件中引入jar包:(如果版本不受限制,可以引用更新的版本)
<!-- webservice start -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf</artifactId>
<version>2.2.10</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>org.apache.neethi</groupId>
<artifactId>neethi</artifactId>
<version>2.0.4</version>
<exclusions>
<exclusion>
<artifactId>xml-apis</artifactId>
<groupId>xml-apis</groupId>
</exclusion>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
<exclusion>
<artifactId>xercesImpl</artifactId>
<groupId>xerces</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- webservice end -->
(4)新建你的webservice的配置文件: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/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- CXF webservice配置 -->
<!-- CXF3以后,只需要引入这个配置文件即可,其他两个废弃掉了-->
<!-- <import resource="classpath:META-INF/cxf/cxf.xml"/> -->
<!-- WebService -->
<!--<jaxws:endpoint id="testService"
implementor="com.rain6.cxf.service.impl.TestWebServiceImpl"
address="/testService"/>-->
<!-- 这里的 serviceClass的值就是源码文件中的唯一的接口类,address的value值就是webservice的地址 ,其他的变量保持不变即可-->
<jaxws:client
id="queryPerCusDetailNewType"
serviceClass="cn.com.cmbc.nps.querypercusdetailnew.QueryPerCusDetailNewType"
address="http://197.3.176.227:15060/queryPerCusDetailNew">
</jaxws:client>
</beans>
ps:这里的<jaxws:client标签应该是被spring识别为一个bean了,所以可以被自动装配并加载。(5)然后在spring初始加载的配置文件中添加这个文件的引用:
例如我们在tesla.xml文件中添加引用:(此处一定要注意,该bean的引用一定要放在加载的controller的bean的前面,不然就会出现错误,因为人家还没加载呢,你就要引用人家,人家当然报错啦)
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:services="http://www.tesla.com/schema/services" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://localhost:8080/schema/www.springframework.org/schema/beans/spring-beans.xsd
http://www.tesla.com/schema/services http://localhost:8080/schema/services.xsd
http://www.springframework.org/schema/context http://localhost:8080/schema/www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://localhost:8080/schema/www.springframework.org/schema/util/spring-util.xsd
">
<!-- 这是应用程序的通用配置,该配置文件以及涉及配置文件中的内容将被加载到Root Spring ApplicationContext之中 -->
<!-- 数据源及事务管理器 -->
<!-- <beans:import resource="common/datasource.xml" /> -->
<beans:import resource="common/multi-datasource.xml" />
<!-- 加载webservice配置文件 -->
<beans:import resource="common/webservice.xml" />
<!-- 安全配置 -->
<beans:import resource="common/security-realm.xml" />
<!-- redis缓存 -->
<beans:import resource="common/redis-jedis-single.xml" />
<!-- 缓存配置,不使用缓存可进行关闭 -->
<!-- <beans:import resource="common/cache.xml" /> -->
<beans:import resource="common/business-commom.xml" />
<!-- 全局dubbo服务配置 -->
<!-- <beans:import resource="services/dubboservices.xml" /> -->
<!-- 单元测试时,把这句话放开,因为单元测试时这个组件不能被加载,所以需要手动配置上;启动tomcat跑时,注释掉。 -->
<!-- <beans:import resource="tesla/internal/tesla-components.xml" /> -->
<!-- 分布式session配置 -->
<!-- <beans:import resource="common/distrubute-session.xml" /> -->
<util:properties id="prop"
location="classpath:/META-INF/commonConfig.properties" />
<!-- 应用通用的MessageSource对象的配置,在MessageSource文件中定义国际化的提示消息 -->
<services:message-source id="messageSource"
defaultEncoding="UTF-8">
<services:basename>classpath:/META-INF/messages/message-source
</services:basename>
</services:message-source>
</beans:beans>
(6)然后即可在controller中调用该webservice的方法了,因为这个service是一个接口,所以我们使用resource引入:
package com.cmbc.cms.action.ApplicationDetail;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.cmbc.cms.dao.ApplicationDetailMapper;
import com.tesla.framework.core.Context;
import com.tesla.framework.function.action.Function;
import com.tesla.framework.function.action.Functions;
import com.tesla.framework.function.action.Reply;
import cn.com.cmbc.nps.querypercusdetailnew.ControlData;
import cn.com.cmbc.nps.querypercusdetailnew.QueryPerCusDetailNewType;
import cn.com.cmbc.nps.querypercusdetailnew.RequestBody;
import cn.com.cmbc.nps.querypercusdetailnew.ResponseBody;
/**
* Created by on 2018/7/31.
*/
@Functions
public class ApplicationDetailSelectAction {
Logger LOG = LoggerFactory.getLogger(this.getClass());
@Autowired
ApplicationDetailMapper applicationDetailMapper;
@Resource
QueryPerCusDetailNewType queryPerCusDetailNewType;
@Function("applicationDetailQuery")
@Reply("result")
public void execute(Context context) {
RequestBody requestBody = new RequestBody();
requestBody.setPartner("1219627168");
ControlData controlData = new ControlData();
controlData.setMedium("0000");
// controlData.setTellerId(value);
controlData.setTransBranch("1234");
controlData.setTransSeqNo("12345678901234567890123456789012");
//controlData.setWsno(value);
requestBody.setControlData(controlData);
ResponseBody responseBody = queryPerCusDetailNewType.queryPerCusDetailNew(requestBody);
System.out.println(responseBody);
}
}