Spring中集成cxf,搭建webservice

      最近由与公司业务的需要,要给一个.net 的系统提供一个webservice接口,之前的框架中是没有集成webservice的,所以就只有自己去搭建了集成了。然后就是在网上各种搜索,都不怎么好使,还好最终搞出来了。

    首先我们去官网下载apache-cxf-2.7.4,这是我下载的版本,解压后,在目录apache-cxf-2.2.4\lib下面有cxf集成spring 需要的所有jar 

第一步:更具自己项目的情况(主要不要让jar包冲突,有的jar就不需要添加了)添加到自己的项目中;

第二步:在项目中写自己需要的发布的接口例如:

使用cxf开发webservice这里只需要在接口上加@webservice注解即可

@WebService
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface WeatherInterface {
	//根据城市名称查询未来三天的天气
	public List<WeatherModel> queryWeather(String cityName) throws Exception;
}

 服务接口实现类:使用cxf开发不用在接口实现类上加@webservice注解,因为cxf发布服务时可以指定接口。

public class WeatherInterfaceImpl implements WeatherInterface {
	
	@Override
	public List<WeatherModel> queryWeather(String cityName) throws Exception {

		//构造三天的天气结果
		Calendar calendar = Calendar.getInstance();
		int day = calendar.get(Calendar.DATE);
		
		WeatherModel weatherModel_1 = new WeatherModel();
		weatherModel_1.setDate(new Date());
		weatherModel_1.setDetail("晴朗");
		weatherModel_1.setTemperature_max(30);
		weatherModel_1.setTemperature_min(23);
		
		WeatherModel weatherModel_2 = new WeatherModel();
		calendar.set(Calendar.DATE, day+1);
		weatherModel_2.setDate(calendar.getTime());
		weatherModel_2.setDetail("晴转多云");
		weatherModel_2.setTemperature_max(28);
		weatherModel_2.setTemperature_min(21);
		
		WeatherModel weatherModel_3 = new WeatherModel();
		calendar.set(Calendar.DATE, day+2);
		weatherModel_3.setDate(calendar.getTime());
		weatherModel_3.setDetail("多云转小雨");
		weatherModel_3.setTemperature_max(25);
		weatherModel_3.setTemperature_min(18);

		List<WeatherModel> list = new ArrayList<WeatherModel>();
		list.add(weatherModel_1);
		list.add(weatherModel_2);
		list.add(weatherModel_3);
			
		return list;
	}
}

第三步:配置applicationContext.xml

刚开始在网上找得各种文章说要在配置文件中加上下面的配置

  1.  <import resource="classpath:META-INF/cxf/cxf.xml" />   
  2.  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  3.  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />   

我没有加这几句,加上反而报错,不知原因下面就是我的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:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/core"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
				            http://www.springframework.org/schema/beans/spring-beans.xsd
				            http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
				            http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
				            http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd">
<!-- 配置发布webservice服务 -->
<jaxws:server address="/weather" serviceClass="cn.itcast.webservice.jaxws.server.WeatherInterface">
      <jaxws:serviceBean>
      	<ref bean="weatherInterface"/>
      </jaxws:serviceBean>
</jaxws:server>

<!-- 公网天气查询客户端 -->
<jaxws:client id="weatherClient" serviceClass="cn.com.webxml.WeatherWebServiceSoap" address="http://www.webxml.com.cn/WebServices/WeatherWebService.asmx" />


<!-- 本系统对外服务的天气查询service -->
<bean id="weatherInterface" class="cn.itcast.webservice.jaxws.server.WeatherInterfaceImpl" >
	<property name="weatherWebServiceSoap" ref="weatherClient" />
</bean>

</beans>

记得在applicationContext.xml 上面加上

xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap"

http://cxf.apache.org/jaxws  http://cxf.apache.org/schemas/jaxws.xsd;

第四步:配置web.xml 文件

web.xml 文件是web项目的入口,所有的请求最先进入的地方就是他了,在这里spring 的配置我就不写了,只写webservice 的配置(说明你的applicationContext.xml 也是要配置在里面的):

<servlet>
        <servlet-name>cxf</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
        <servlet-name>cxf</servlet-name>
        <url-pattern>/ws/*</url-pattern>
</servlet-mapping>

然后启动项目:访问:http://localhost:port/.../ws/weather?wsdl

猜你喜欢

转载自my.oschina.net/u/3267498/blog/1602255