maven项目springmvc集成cxf的webservice

本文详细介绍如何使用Apache CXF在Spring环境中搭建WebService,包括Maven依赖配置、XML配置、接口及其实现类创建、web.xml配置等关键步骤,解决常见问题如版本冲突、服务未找到等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本流程和前面cxf开发webservice的那篇博客差不多,https://blog.youkuaiyun.com/heqinghua217/article/details/50820250

 

1、maven引入jar文件如下(pom.xml):(这里遇到了一个坑,切记spring如果用的是4.0以上的版本,那么cxf就不能低于3.0,否则会报错) (第一次用maven,如果你去maven仓库搜索cxf,会搜索到很多,以前你可能用的是一个大的jar,就叫cxf-2.2.0,但是maven没有这个jar,而是分了很多,你如果不知道用哪个,你就按照排名来,以及大小来,然后如果发现少了什么,再根据引用到的包名去加载即可。)

<properties>
    	<cxf.version>3.2.0</cxf.version>
	</properties>

<!-- webservice  start -->
		<dependency> 
		         <groupId>org.apache.cxf</groupId> 
		         <artifactId>cxf-rt-frontend-jaxws</artifactId> 
		         <version>${cxf.version}</version> 
		</dependency> 
		<dependency> 
		         <groupId>org.apache.cxf</groupId> 
		         <artifactId>cxf-rt-transports-http</artifactId> 
		         <version>${cxf.version}</version> 
		</dependency> 
		<dependency> 
		         <groupId>org.apache.cxf</groupId> 
		         <artifactId>cxf-rt-transports-http-jetty</artifactId> 
		         <version>${cxf.version}</version> 
		</dependency> 
		<dependency> 
		        <groupId>commons-httpclient</groupId> 
		        <artifactId>commons-httpclient</artifactId> 
		        <version>3.0</version> 
		</dependency>
	    <!-- webservice  end -->

2、创建一个普通的xml文件,用于定义cxf服务端的配置,名称你自己随便写,我写成htwebservice.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.huating.outService.Test12333WSIMP"> </bean>
	<jaxws:server id="test12333WSServer" address="/test12333WSServer">
		<jaxws:serviceBean>
			<ref bean="test12333WS" />
		</jaxws:serviceBean>
	</jaxws:server>
	<!-- 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>

<bean id="test12333WS" class="com.huating.outService.Test12333WSIMP"> </bean>
    <jaxws:server id="test12333WSServer" address="/test12333WSServer">
        <jaxws:serviceBean>
            <ref bean="test12333WS" />
        </jaxws:serviceBean>
    </jaxws:server>

com.huating.outService.Test12333WSIMP 这个是你的webservice的接口实现类:包名.类名

test12333WSServer 这个名字对应实现类中的servicename注解

test12333WSServer 这个名字是对外的接口路径名称,你自己怎么开心怎么来

 

3、java接口类

package com.huating.outService;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/*
/*注意:1、@WebService必须要写,否则会找不到接口方法、
 *     2、接口Test12333WS放的路径,在什么包下要和服务端一模一样,否则也会报错
 * 
 * 
 * */
@WebService
public interface Test12333WS {
	//使用@WebMethod注解标注WebServiceI接口中的方法
    @WebMethod
	public String say(@WebParam(name="name") String name);
    

}

4、接口实现类

package com.huating.outService;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.jws.WebService;

@WebService(endpointInterface="com.huating.outService.Test12333WS",serviceName="test12333WSServer")
public class Test12333WSIMP implements Test12333WS {

	public String say(String name){
		System.out.println(name +"Hello World");
		return name +"Hello World";
	}
	 

	
}

@WebService(endpointInterface="com.huating.outService.Test12333WS",serviceName="test12333WSServer")

test12333WSServer就是第二步骤中的htwebservice.xml中的  <jaxws:server id="test12333WSServer"  >

com.huating.outService.Test12333WS 这个是接口类

后面亲测,下面这一串代码不用也可以

@WebService(endpointInterface="com.huating.outService.Test12333WS",serviceName="test12333WSServer")

 

5、web.xml(这里又遇到了一个坑,很严重的坑,报的是找不到服务,是英文的报错,所以加入了ContextLoaderListenner,以及context-param )

 <!-- 为了cxf而加入 start  -->
 	<context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>   
           classpath*:htwebservice.xml 
        </param-value>   
    </context-param> 
    <listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 为了cxf而加入 end  -->
	<!-- WebService start -->
	<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>
	<!-- WebService end -->

整个web.xml文件是这样的

<?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*:htwebservice.xml 
        </param-value>   
    </context-param> 
    <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 start -->
	<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>
	<!-- WebService end -->

	<!-- post编码 -->
	<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>

原来的写法是没有这一串代码:


   <!-- 为了cxf而加入 start  -->
     <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>   
           classpath*:htwebservice.xml 
        </param-value>   
    </context-param> 
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- 为了cxf而加入 end  -->


 然后吧 classpath*:htwebservice.xml  放到了如下位置,结果一直报错no services have been found,唉,百度找到了这样一段话,终于解决了,望大家不要再犯这样的错误。(

原因就是:Spring MVC是通过DispatcherServlet来加载Spring配置文件的,因此不需要在web.xml中配置ContextLoaderListener。但是CXF却需要通过ContextLoaderListener来加载Spring。这样就产生了一个矛盾,如果不配置ContextLoaderListener,CXF就无法正常使用。但如果配置ContextLoaderListener,又会造成Spring的重复加载(DispatcherServlet一次,ContextLoaderListener一次)

为了同时能够实现使用两个加载器,又能够不重复加载,我们需要将两者的配置文件分离开来。这也是我们为啥将spring-cxf.xml分离开dispatcher-servlet.xml来写

<!-- 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,classpath*:htwebservice.xml </param-value> 
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

 

 

最后项目跑起来,访问 http://ip:端口/项目名称/services 即可    这里写的是services因为在web.xml里配置的cxf的servlet

<!-- WebService start -->
    <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>
    <!-- WebService end -->

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值