Maven项目SpringMvc集成CXF实现WebService接口

开发Webservice接口可以使用Axis2,也可以使用CXF。可以参考下面几篇博客(主要讲述Axis2开发Webservice接口):

Axis2在eclipse中搭建

在Eclipse中使用Axis2插件生成Web Service服务端/客户端

Axis2与Web项目整合及Axis2在Web项目中整合Spring

利用AXIS2开发Webservice接口,浏览器访问返回纯JSON数据,没有xml标识

下面介绍Maven+SpringMVC+CXF实现Webservice接口。

第一步:

众所周知Maven管理了所有的JAR包,所以首先在pom.xml文件中引入Jar包。(这里遇到了一个坑,切记spring如果用的是4.0以上的版本,那么cxf就不能低于3.0,否则会报错

POM.XML中需要增加如下配置:

        <!-- 集成CXF实现Webservice接口 -->
        <dependency>
            <groupId>wsdl4j</groupId>
            <artifactId>wsdl4j</artifactId>
            <version>1.6.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-frontend-jaxws</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http</artifactId>
            <version>3.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.cxf</groupId>
            <artifactId>cxf-rt-transports-http-jetty</artifactId>
            <version>3.2.0</version>
        </dependency>

第二步:创建Webservice接口类

CxyJkfwService.java 

package com.inspur.tax.yjglpt.webserviceKfptService.service;

import javax.jws.WebService;

/**
 * 
 * @Description: webservice服务
 * @author fuxs
 * @date 2016-2-2 下午2:46:33
 *
 */
@WebService
public interface CxyJkfwService {
	public String swapInfo(String requestXml) throws Exception;
}

CxyJkfwServiceImpl.java实现CxyJkfwService接口继承SpringBeanAutowiringSupport

package com.inspur.tax.yjglpt.webserviceKfptService.service;

import javax.jws.WebService;

import org.springframework.web.context.support.SpringBeanAutowiringSupport;


@WebService(endpointInterface = "com.inspur.tax.yjglpt.webserviceKfptService.service.CxyJkfwService",serviceName="cxyJkfwService")
public class CxyJkfwServiceImpl extends SpringBeanAutowiringSupport implements CxyJkfwService{


	@Override
	public String swapInfo(String requestXml) throws Exception {
		String xml  = "我爱你中华";
		return xml;
		
	}
	
}

 

第三步:创建一个普通的xml文件,用于定义cxf服务端的配置

名称你自己随便写,我这里叫:cxf-servlet.xml

(这里注释了此段代码,cxf3.0以上不需要此xml文件了,否则会有警告<!-- 新版本说不需要此xml文件 <import resource="classpath:META-INF/cxf/cxf-extension-soap.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:http-conf="http://cxf.apache.org/transports/http/configuration"
	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
						http://cxf.apache.org/transports/http/configuration 
             			http://cxf.apache.org/schemas/configuration/http-conf.xsd">  
	
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	<!-- 新版本说不需要此xml文件 <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> -->
 	
	<!-- Start 供WebService调用的业务Service -->
	<bean id="test12333WS" class="com.inspur.tax.yjglpt.webserviceKfptService.service.CxyJkfwServiceImpl"> </bean>
	<jaxws:server id="cxyJkfwService" address="/cxyJkfwService">
		<jaxws:serviceBean>
			<ref bean="test12333WS"/>
		</jaxws:serviceBean>
	</jaxws:server>
	
	<!-- cui_xyang 解析说明:class: 这个是你的webservice的接口实现类:包名.类名
		address: 这个名字是对外的接口路径名称,你自己怎么开心怎么来
		jaxws中id:这个名字对应实现类中的servicename注解 -->
	<!-- End 供WebService调用的业务Service-->
	
	
	<!-- Start  End 客户端使用  
	 <bean id="client"   
            class="com.huating.outService.Test12333WS"   
            factory-bean="clientFactory"   
            factory-method="create"/>  
  
     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">  
            <property name="serviceClass" value="com.huating.outService.Test12333WS"/>  
            <property name="address" value="http://localhost:8080/yzxx/services/test12333WS"/>  
     </bean>  -->
       
	 
</beans>
 

第四步:web.xml加载cxf-servlet.xml,配置CXF

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>manager</display-name>
 
    <!-- 为了cxf而加入 start  -->
 	<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>   
           classpath*:cxf-servlet.xml
        </param-value>   
    </context-param> 
    <!-- Spring监听配置,Web容器启动自动装配ApplicationContext的配置信息-->
    <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 为了cxf而加入 end  -->
	
	<listener>
		<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
	</listener>
	
	<!-- springmvc配置 -->
	<servlet>
		<servlet-name>springweb</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			  <param-value>classpath:spring-context.xml</param-value> 
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springweb</servlet-name>
		<url-pattern>*.shtml</url-pattern>
	</servlet-mapping>
	
	<!--webservice服务端 cxy-->
	<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/yshdjkptApi/*</url-pattern>
	</servlet-mapping>
 
	<!-- Spring字符编码过滤器配置,处理中文乱码 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>*.shtml</url-pattern>
	</filter-mapping>
	
	<error-page>
		<error-code>404</error-code>
		<location>/404.shtml</location>
	</error-page>
	<welcome-file-list>
		<welcome-file>/login.shtml</welcome-file>
	</welcome-file-list>
</web-app>

测试WebService接口

 

最后请各位关注一下公众号,会不定时推送一些开发中碰到的问题的解决方法,以及会分享一些开发视频。资料等。公众号里有联系方式,大家可以一起交流分享开发中遇到的问题,以及总结的经验,请大家关注一下谢谢:

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李晓LOVE向阳

你的鼓励是我持续的不断动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值