Spring中集成cxf,搭建webservice

      最近由與公司業務的須要,要給一個.net 的系統提供一個webservice接口,以前的框架中是沒有集成webservice的,因此就只有本身去搭建了集成了。而後就是在網上各類搜索,都不怎麼好使,還好最終搞出來了。java

    首先咱們去官網下載apache-cxf-2.7.4,這是我下載的版本,解壓後,在目錄apache-cxf-2.2.4\lib下面有cxf集成spring 須要的全部jar web

第一步:更具本身項目的狀況(主要不要讓jar包衝突,有的jar就不須要添加了)添加到本身的項目中;spring

第二步:在項目中寫本身須要的發佈的接口例如:apache

使用cxf開發webservice這裏只須要在接口上加@webservice註解便可app

@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.xmlide

剛開始在網上找得各類文章說要在配置文件中加上下面的配置url

  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配置文件spa

<?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 上面加上.net

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

相關文章
相關標籤/搜索