加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

运用CXF开发webService接口服务端和客户端

发布时间:2020-12-16 22:59:36 所属栏目:安全 来源:网络整理
导读:webService服务端 1 pom.xml文件内容 project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" ?

webService服务端

1 pom.xml文件内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
? <modelVersion>4.0.0</modelVersion>
? <groupId>merchants_webService</groupId>
? <artifactId>merchants_webService</artifactId>
? <packaging>war</packaging>
? <version>0.0.1-SNAPSHOT</version>
? <name>merchants_webService</name>
? <url>http://maven.apache.org</url>
? <dependencies>
? ? <dependency>
? ? ? <groupId>junit</groupId>
? ? ? <artifactId>junit</artifactId>
? ? ? <version>3.8.1</version>
? ? ? <scope>test</scope>
? ? </dependency>
? ? <!-- spring、和mybatis搭建框架依赖的jar -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.struts.xwork</groupId>
<artifactId>xwork-core</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j</artifactId>
<version>2.0-alpha1</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.annotation</artifactId>
<version>3.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.16.1-GA</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.3</version>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>cn.guoyukun.jdbc</groupId>
<artifactId>oracle-ojdbc6</artifactId>
<version>11.2.0.3.0</version>
</dependency>
<dependency>
<groupId>wsdl4j</groupId>
<artifactId>wsdl4j</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-soap</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-bindings-xml</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-databinding-jaxb</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-simple</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-common</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-ws-addr</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-tools-common</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-api</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-common-utilities</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>javax.jws</groupId>
<artifactId>jsr181-api</artifactId>
<version>1.0-MR1</version>
</dependency>
? </dependencies>
? <build>
? ? <finalName>merchants_webService</finalName>
? </build>
</project>


2 web.xml文件内容

<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet> ?
? ? ? ? <servlet-name>CXFServlet</servlet-name> ?
? ? ? ? <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> ?
? ? ? ? <load-on-startup>1</load-on-startup> ?
? ? </servlet> ?
? ? <servlet-mapping> ?
? ? ? ? <servlet-name>CXFServlet</servlet-name> ?
? ? ? ? <url-pattern>/services/*</url-pattern> ?
? ? </servlet-mapping>


<display-name>test</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>

<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<!--?
//该路径为相对于WEB应用程序根路径的位置,必须以/开头.
-->
<error-page>
<error-code>404</error-code>
<location>/error_404.jsp</location>
</error-page>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


3?applicationContext.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"?
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"?
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans?
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/aop?
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/tx?
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context?
http://www.springframework.org/schema/context/spring-context-3.1.xsd"
default-autowire="byName">

<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor">
<property name="fallbackToDefaultTypeMatch" value="false"/>
</bean>
<!--配置数据源属性文件 ?--> ?
? ? <bean id="propertyConfigurer" ?
? ? ? ? class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> ?
? ? ? ? <property name="location"> ?
? ? ? ? ? ? <value>classpath:com.properties</value> ?
? ? ? ? </property> ?
? ? </bean>


<!--配置数据源 ?--> ?
? ? <bean id="dataSource" ?
? ? ? ? class="org.apache.commons.dbcp.BasicDataSource"> ?
? ? ? ? <property name="driverClassName"> ?
? ? ? ? ? ? <value>${jdbc.driver}</value> ?
? ? ? ? </property> ?
? ? ? ? <property name="url"> ?
? ? ? ? ? ? <value>${jdbc.url}</value> ?
? ? ? ? </property> ?
? ? ? ? <property name="username"> ?
? ? ? ? ? ? <value>${jdbc.user}</value> ?
? ? ? ? </property> ?
? ? ? ? <property name="password"> ?
? ? ? ? ? ? <value>${jdbc.pwd}</value> ?
? ? ? ? </property> ?
? ? </bean> ?
? ??
? ? <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> ? ?
? ? <property name="dataSource" ref="dataSource" />?
? ? ? ? <property name="configLocation" value="classpath:mybatis-config.xml" /> ? ?
? ? </bean>
? ??
? ? <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!-- CXF的webService接口配置 -->
<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" /> ?
? <bean id="merchants" class="com.netfinworks.webservice.service.MerchantsService"/> ?
? ? <jaxws:endpoint id="merchantsService" implementor="#merchants" address="/MerchantsService" />


<!-- 事务权限配置 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" read-only="true" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 利用spring切面编程进行事务管理,给哪些包下的方法使用事务-->
<aop:config>
<aop:pointcut id="interceptorPointCuts" expression="execution(* com.netfinworks.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
</aop:config>
? ??
<!-- ********** ?利用spring的注解注入方式 ?***************** -->
<context:component-scan base-package="com.netfinworks.webservice.service">
</context:component-scan>
<context:component-scan base-package="com.netfinworks.dao">
</context:component-scan>

</beans>


4?cxf.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!--
? Licensed to the Apache Software Foundation (ASF) under one
? or more contributor license agreements. See the NOTICE file
? distributed with this work for additional information
? regarding copyright ownership. The ASF licenses this file
? to you under the Apache License,Version 2.0 (the
? "License"); you may not use this file except in compliance
? with the License. You may obtain a copy of the License at
?
? http://www.apache.org/licenses/LICENSE-2.0
?
? Unless required by applicable law or agreed to in writing,
? software distributed under the License is distributed on an
? "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY
? KIND,either express or implied. See the License for the
? specific language governing permissions and limitations
? under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
? ? <!-- ?For Testing using the Spring commons processor,uncomment one of:-->
? ? <!--?
? ? ? ? ? ? ? ? <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
? ? ? ? ? ? ? ? <context:annotation-config/>
? ? ? ? -->
? ? <bean id="cxf" class="org.apache.cxf.bus.spring.SpringBus" destroy-method="shutdown"/>
? ? <bean id="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor" class="org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor"/>
? ? <bean id="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor" class="org.apache.cxf.bus.spring.Jsr250BeanPostProcessor"/>
? ? <bean id="org.apache.cxf.bus.spring.BusExtensionPostProcessor" class="org.apache.cxf.bus.spring.BusExtensionPostProcessor"/>
</beans>


5?cxf-extension-soap.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!--
? Licensed to the Apache Software Foundation (ASF) under one
? or more contributor license agreements. See the NOTICE file
? distributed with this work for additional information
? regarding copyright ownership. The ASF licenses this file
? to you under the Apache License,either express or implied. See the License for the
? specific language governing permissions and limitations
? under the License.
-->


<beans xmlns="http://www.springframework.org/schema/beans"
? ? ? ?xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
? ? ? ?xmlns:foo="http://cxf.apache.org/configuration/foo"
? ? ? ?xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
? ??
? ? <bean id="org.apache.cxf.binding.soap.SoapBindingFactory"
?class="org.apache.cxf.bus.spring.OldSpringSupport" lazy-init="true">
? ? ? ? <constructor-arg ref="cxf"/>
? ? ? ? <constructor-arg value="org.apache.cxf.binding.soap.SoapBindingFactory"/>
? ? ? ? <constructor-arg value="cxf-extension-soap.xml"/>
? ? </bean>
? ? <bean id="org.apache.cxf.binding.soap.SoapTransportFactory"
? ? ? class="org.apache.cxf.bus.spring.OldSpringSupport" lazy-init="true">
? ? ? ? <constructor-arg ref="cxf"/>
? ? ? ? <constructor-arg value="org.apache.cxf.binding.soap.SoapTransportFactory"/>
? ? </bean>
? ??
? ? ? ??
<bean id="org.apache.cxf.binding.soap.customEditorConfigurer"
class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="propertyEditorRegistrars">
<list>
<bean class="org.apache.cxf.binding.soap.spring.SoapVersionRegistrar"/>
</list>
</property>
</bean>
</beans>


6?cxf-servlet.xml文件内容

<?xml version="1.0" encoding="UTF-8"?>
<!--
? Licensed to the Apache Software Foundation (ASF) under one
? or more contributor license agreements. See the NOTICE file
? distributed with this work for additional information
? regarding copyright ownership. The ASF licenses this file
? to you under the Apache License,either express or implied. See the License for the
? specific language governing permissions and limitations
? under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:foo="http://cxf.apache.org/configuration/foo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">




</beans>


8 接口类文件IMerchantsService

package com.netfinworks.webservice.service;


import javax.jws.WebService;


import com.netfinworks.webservice.service.bean.MerchantsInfo;


/**
?* 类说明:二级商户信息Service
?*?
?* @author?
?* @date 2015-3-13
?*/
@WebService
public interface IMerchantsService {


/**
* 查询二级商户信息
*?
* @param merchantsId
* ? ? ? ? ? ?二级商户编号
* @param bankId
* ? ? ? ? ? ?银行编号
* @return
*/
public MerchantsInfo queryMerchantsInfo(String merchantsId,String bankId);



}



9 接口具体实现类文件MerchantsService

package com.netfinworks.webservice.service;


import javax.jws.WebService;


import org.apache.commons.lang.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.netfinworks.dao.MerchantsDAO;
import com.netfinworks.webservice.service.bean.MerchantsInfo;


/**
?* 类说明:二级商户信息Service实现
?*?
?* @author?
?* @date 2015-3-13
?*/
@WebService(endpointInterface="com.netfinworks.webservice.service.IMerchantsService")
public class MerchantsService implements IMerchantsService {


public static ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"applicationContext.xml");


protected static final Logger log = LogManager
.getLogger(MerchantsService.class);


public MerchantsInfo queryMerchantsInfo(String merchantsId,String bankId) {
log.info("queryMerchantsInfo接口参数: n" + "merchantsId: " + merchantsId
+ "n" + "bankId: " + bankId);
if (StringUtils.isBlank(merchantsId)) {
MerchantsInfo info = new MerchantsInfo();
info.setResCode("111");
info.setResMsg("参数[merchantsId]不能为空!");
log.info("参数[merchantsId]为空!");
return info;
}
MerchantsDAO dao = applicationContext.getBean("merchantsDAO",
MerchantsDAO.class);
try {
MerchantsInfo info = dao.queryMerchantsInfo(merchantsId,bankId);
if (info == null) {
info = new MerchantsInfo();
info.setResCode("000");
info.setResMsg("XXX");
info.setMerchantsId("000000000001");
info.setMerchantsName("XXX");
info.setMerchantsTypeName("XXX");
info.setBankId("CMB");
return info;
} else {
info.setResCode("000");
info.setResMsg("查询成功");
if (StringUtils.isNotBlank(info.getTypeNameDetail())) {
info.setMerchantsTypeName(info.getMerchantsTypeName() + "_"
+ info.getTypeNameDetail());
}
return info;
}
} catch (Exception e) {
log.error(e);
MerchantsInfo info = new MerchantsInfo();
info.setResCode("222");
info.setResMsg("查询失败");
return info;
}
}





}


webService客户端

1 根据wsdl访问地址,新建一个java Project项目,运用myeclipse中的webService client生成工具,生成客户端代码。


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读