WebService跨平台调用

本文介绍如何通过CXF框架部署WebService,并解决了Struts2与CXF可能产生的冲突问题。同时,提供了详细的配置步骤及如何使用wsimport命令生成客户端调用代码。

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

导入CXF相关的jar包

在  web.xml  中加入 CXF Service  配置:

<servlet>
     <servlet-name>cxfServlet</servlet-name>
     <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
  </servlet>
  <servlet-mapping>
     <servlet-name>cxfServlet</servlet-name>
     <url-pattern>/cxfTest/*</url-pattern>
  </servlet-mapping>   

在配置struts2的拦截器时要注意可能会与CXF冲突:

<filter>
      <filter-name>struts2</filter-name>
     <filter-class>com.xxxxxxxx.Struts2Filter</filter-class>
  </filter>
  <filter-mapping>
      <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
  </filter-mapping>

struts2会拦截cxf的请求,解决方法使用自定义的拦截器:

public class Struts2Filter extends StrutsPrepareAndExecuteFilter {

	public void doFilter(ServletRequest req, ServletResponse response, FilterChain chain)throws IOException, ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		if(request.getRequestURI().contains("/cxfTest")){
			chain.doFilter(request, response);
		}else{
			super.doFilter(request, response, chain);
		}

	}
	
}

application-cxf.cml:

<?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:cxf="http://cxf.apache.org/core"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd">
	<bean id="weatherService" class="com.********.WeatherService">
	</bean>	
	
	<jaxws:server address="/weather">
	    <jaxws:serviceBean >
	         <ref bean="weatherService" />
	    </jaxws:serviceBean>
	</jaxws:server>
	
	
		

</beans>

发布webService之后:

使用

wsimport -s .  http://localhost:8888/WeatherService/cxfTest/weather?wsdl

命令来生成方法。

配置application-cxf.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:cxf="http://cxf.apache.org/core"
	xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://cxf.apache.org/core
        http://cxf.apache.org/schemas/core.xsd
        http://cxf.apache.org/jaxws
        http://cxf.apache.org/schemas/jaxws.xsd
        http://cxf.apache.org/jaxrs
        http://cxf.apache.org/schemas/jaxrs.xsd
        ">
     <description>CXF</description>

	<jaxws:client id="weatherClient" address="http://localhost:8888/WeatherService/cxfTest/weather?wsdl" serviceClass="com.*****.WeatherService"></jaxws:client>


</beans>

利用生成的接口进行调用即可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值