webservice發布服務:CXF及用戶端調用

來源:互聯網
上載者:User

標籤:ipa   []   匯入   自訂   音樂人   address   客戶   public   使用   

2.CXF:(與spring整合)CXF相對來說操作沒有AXIS繁瑣1.匯入spring的jar包和cxf的jar包2.在spring的核心設定檔中配置發布的介面類<?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-3.0.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">     <!-- 引入CXF Bean定義如下,早期的版本中使用 --> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />     <!--        通過設定檔發布一個不帶介面的webservice       1:id       2:提供服務的訪問地址       3:提供服務的類...      --> <jaxws:endpoint id="springService" address="/springService" implementor="cn.itcast.cxf.web.SpringServiceHello"> </jaxws:endpoint>  <!-- 我的天氣預報 --> <!-- <jaxrs: id="weather" address="/weather" serviceClass="cn.itcast.cxf.web.Weather">  </jaxrs:server> --> <jaxws:endpoint id="weather" address="/weather" implementor="cn.itcast.cxf.web.Weather"> </jaxws:endpoint> <!-- 音樂人 --> <jaxws:endpoint id="music" address="/music" implementor="cn.itcast.cxf.web.Music"> </jaxws:endpoint>   <!-- 發布一個帶介面的webservice       1:id      2:提供服務的訪問地址      3:介面的類型 -->  <jaxws:server id="ipaddress" address="/ipaddress" serviceClass="cn.itcast.cxf.web.IpAddressService">   <jaxws:serviceBean>    <!-- 介面的實作類別... -->    <bean class="cn.itcast.cxf.web.IpAddressServiceImpl"></bean>   </jaxws:serviceBean>   <!-- 請求的訊息攔截器 -->   <!-- <jaxws:inInterceptors>    <bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>   </jaxws:inInterceptors> -->   <!-- 響應的訊息攔截器... -->   <!-- <jaxws:outInterceptors>   <bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>   </jaxws:outInterceptors> --> </jaxws:server>   </beans>3.在web.xml中配置cxf的servlet過濾器<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"  xmlns="http://java.sun.com/xml/ns/javaee"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  <!-- 通過spring 的listener 去解析 cxf-Servlet 設定檔... --> <!-- <listener>  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> -->   <context-param>  <param-name>contextConfigLocation</param-name>  <param-value>/WEB-INF/cxf-servlet.xml</param-value> </context-param>   <!--    第一次訪問CXFServlet 然後/WEB-INF/cxf-servlet.xml 交給 init 方法 去解析...  cxf 與spring 進行無縫的整合,調用spring.jar 包裡面的類... 解析 cxf-servlet.xml 設定檔...拿到設定檔裡面的資訊..   <jaxws:server id="ipaddress" address="/ipaddress" serviceClass="cn.itcast.cxf.web.IpAddressService">      <jaxws:serviceBean>    介面的實作類別...    <bean class="cn.itcast.cxf.web.IpAddressServiceImpl"></bean>    JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean();  request.    http://localhost:8080/cxfspringweb/services  + /ipaddress    bean.setAddress(http://localhost:8080/cxfspringweb/services  + /ipaddress);    //介面  通過反射拿到cn.itcast.cxf.web.IpAddressService.class  bean.setServiceClass();    bean.ServiceBean(Class.forName("cn.itcast.cxf.web.IpAddressServiceImpl").newInstance());    bean.create();    -->  <servlet>        <servlet-name>cxf</servlet-name>        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>        <init-param>         <param-name>config-location</param-name>         <param-value>/WEB-INF/cxf-servlet.xml</param-value>        </init-param>    </servlet>    <servlet-mapping>        <servlet-name>cxf</servlet-name>        <url-pattern>/services/*</url-pattern>    </servlet-mapping>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list></web-app>啟動伺服器,訪問wsdl文檔。如果訪問成功說明介面發布成功產生用戶端程式碼:使用jdk命令wsimport -s . -p cn.itcast http://localhost:8080/axis2Server/services/testService?wsdl產生用戶端代碼,也可將此語句放置在自訂的bat檔案中執行用戶端代碼調用:(通過設定檔調用)建立beanssss.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-3.0.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">       <!-- 通過設定檔調用... 通過這種方式調用需要依賴一個介面... -->       <jaxws:client id="itcast" address="http://192.168.9.12:8080/cxfspringweb/services/ipaddress?wsdl" serviceClass="com.baidu.config.IpAddressService">       </jaxws:client>              <jaxws:client id="weather" address="http://192.168.9.12:8080/cxfspringweb/services/weather?wsdl" serviceClass="cn.itcast.weather.Weather">       </jaxws:client>              <jaxws:client id="music" address="http://192.168.9.12:8080/cxfspringweb/services/music?wsdl" serviceClass="cn.itcast.music.Music">       </jaxws:client></beans>通過bean的方式調用:package cn.itcast.spring.web;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import cn.itcast.music.Music;import cn.itcast.weather.Weather;import com.baidu.config.IpAddressService;/** *  * 通過設定檔來調用webservice 服務... * @author Administrator * */public class SpringConfigInvoke { /*public Weather weather;  public Weather getWeather() {  return weather; } public void setWeather(Weather weather) {  this.weather = weather; }*/  /**  * @param args  */ public static void main(String[] args) {  ApplicationContext context=new ClassPathXmlApplicationContext("beanssss.xml");  //根據IP查詢所在地  IpAddressService addressService=(IpAddressService) context.getBean("itcast");  String ipaddress=addressService.getAddressByIp("192.168.9.12");  System.out.println(ipaddress);    //我的天氣  /*SpringConfigInvoke springConfigInvoke = new SpringConfigInvoke();  springConfigInvoke.weather();*/    Weather weather = (Weather) context.getBean("weather");  System.out.println(weather.bjWeather());    //音樂人  Music music = (Music) context.getBean("music");  System.out.println(music.singer()); }  /*public void weather(){   weather.bjWeather(); }*/}

 

webservice發布服務:CXF及用戶端調用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.