ice world

          There is nothing too difficult if you put your heart into it.
          posts - 104, comments - 103, trackbacks - 0, articles - 0

          CXF+Spring+Tomcat簡明示例

          Posted on 2012-07-06 17:29 IceWee 閱讀(51161) 評論(27)  編輯  收藏 所屬分類: Others 、Tomcat
          多系統(異構系統)進行交互時,一種良好的方式便是調用Web Service,本示例基于Apache組織的CXF,為了方便起見特將服務端和客戶端寫在同一個工程下,實際項目中是不可能的,但是客戶端卻依賴于服務端的Web Service接口,那么可以通過導出jar的方式。

          環境:
          MyEclipse10
          JDK6
          Tomcat7
          CXF2.5
          Spring3

          示例項目結構圖:


          如上圖所示,全部依賴的第三方庫都在lib中,下面貼出全部代碼。
          IHelloService.java
          package bing.server;

          import javax.jws.WebService;

          /**
          * <p>
          * WebService接口
          * </p>
          *
          *
          @author IceWee
          * @date 2012-7-6
          *
          @version 1.0
          */

          @WebService
          public interface IHelloService {

             
          public String sayHello(String username);
             
          }


          HelloServiceImpl.java
          package bing.server;

          import javax.jws.WebService;

          /**
          * <p>
          * WebService實現類
          * </p>
          *
          *
          @author IceWee
          * @date 2012-7-6
          *
          @version 1.0
          */

          @WebService(endpointInterface
          = "bing.server.IHelloService", serviceName = "HelloService")
          public class HelloServiceImpl implements IHelloService {

              @Override
             
          public String sayHello(String username) {
                 
          return "hello, " + username;
              }


          }


          HelloServiceClient.java
          package bing.client;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          import bing.server.IHelloService;

          /**
          * <p>
          * WebService調用方-客戶端
          * </p>
          *
          *
          @author IceWee
          * @date 2012-7-6
          *
          @version 1.0
          */

          public class HelloServiceClient {

             
          public static void main(String[] args) {
                  ApplicationContext context
          = new ClassPathXmlApplicationContext("applicationContext-client.xml");
                  IHelloService helloService
          = (IHelloService) context.getBean("client");
                  String response
          = helloService.sayHello("Peter");
                  System.out.println(response);
              }


          }


          applicationContext-server.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"
              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"
          >
             
          <!--
                  ***注意***
                  手動添加的內容:
                  xmlns:jaxws="http://cxf.apache.org/jaxws"
                  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
              
          -->
             
             
          <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" />

             
          <jaxws:endpoint id="helloService" implementor="bing.server.HelloServiceImpl" address="/helloService" />
                 
          </beans>

          applicationContext-client.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"
              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"
          >
             
          <!--
                  ***注意***
                  手動添加的內容:
                  xmlns:jaxws="http://cxf.apache.org/jaxws"
                  http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"
              
          -->
             
             
          <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" />

             
          <bean id="client" class="bing.server.IHelloService" factory-bean="clientFactory" factory-method="create" />

             
          <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">
                 
          <property name="serviceClass" value="bing.server.IHelloService" />
                 
          <property name="address" value="http://localhost:8080/CXFDemo/ws/helloService" />
             
          </bean>
          </beans>

          web.xml
          <?xml version="1.0" encoding="UTF-8"?>
          <web-app version="3.0"
              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_3_0.xsd"
          >
           
          <display-name>CXFDemo</display-name>
           
           
          <context-param>
               
          <param-name>contextConfigLocation</param-name>
               
          <param-value>classpath:applicationContext-server.xml</param-value>
           
          </context-param>

           
          <listener>
             
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
           
          </listener>
           
           
          <servlet>
             
          <servlet-name>CXFServlet</servlet-name>
             
          <display-name>CXFServlet</display-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>/ws/*</url-pattern>
           
          </servlet-mapping>
           
          <welcome-file-list>
             
          <welcome-file>index.jsp</welcome-file>
           
          </welcome-file-list>
          </web-app>

          所有項目都已配置完成,可以發布到Tomcat了,在瀏覽器中輸入:http://localhost:8080/CXFDemo/ws,返回如圖:


          從上圖中可以看到我們對外發布的WebService接口,點擊藍色超鏈接,返回如圖:

          到此,證明我們的Web Service已經發布成功,可以進行調用測試了。運行HelloServiceClient,返回如圖:


          全文完!


























          Feedback

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2012-11-06 14:54 by 林祥
          <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" />

          這些文件沒有啊

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2012-11-27 13:09 by 狼湖
          HelloServiceImpl 類的注解:
          @WebService(endpointInterface = "bind.server.IHelloService", serviceName = "helloService")

          注意:serviceName 是小寫開頭的helloService否則出錯

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2012-11-27 13:10 by 狼湖
          @林祥
          那幾個xml在cxf-xxx.jar包里面

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2012-11-28 11:07 by qss
          1、非常感謝樓主,我運行成功了。
          2、我運行時報錯誤:
          我看了原因是jar不全,我將apache-cxf-2.7.0解壓后的lib中的jar包全部引入就好了。
          3.再次感謝樓主!//:

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2012-11-28 11:24 by IceWee
          @qss
          ^_^

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2012-11-28 11:53 by qss
          1.IceWee,你好。
          我想問一下,我返回的是對象時,對象有個屬性error_message,部署tomcat時,報:類的兩個屬性具有相同名稱 "error_message" ?

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2012-11-28 15:32 by qss
          IceWee,我已經解決了,^_^

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2013-01-16 10:25 by Ameri
          謝謝

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2013-03-09 15:44 by asfas d
          在網上找了兩天 最后還是在你這里把問題解決了 能不能再寫個傳復雜數據的例子呀LZ

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2013-03-13 10:39 by 周炎婷
          你好,我想問一下,如何運行客戶端?

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2013-04-26 23:16 by 畢淑敏
          好帖子,比網上n個cxf實例都強!

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2013-08-06 15:55 by derekwu
          少了 Woodstox這個包,否則報錯java.lang.RuntimeException: Cannot create a secure XMLInputFactory

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2013-08-07 13:59 by 開發吧
          博主能否提供源碼?。恐x謝

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2013-11-25 11:31 by 3000
          @derekwu
          加了Woodstox之后,報找不到org/codehaus/stax2/XMLInputFactory2類,又加了stax2-api-3.1.1.jar 才好。
          我用的apache-cxf-2.6.10

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2013-12-03 14:52 by 啊啊啊啊
          啊啊啊啊啊啊啊啊

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2014-01-20 15:56 by 求知者 QQ 285678313
          我的applicationContext-server.xml編譯不過,“jaxws:endpoint ”那一行報錯,錯誤信息是“Failed to read schema document ‘http://cxf.apache.org/schemas/jaxws.xsd’, because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

          說找不到jaxws.xsd文件,我的網絡良好,這個URL放在瀏覽器里可以訪問到,求解,上面找不到你的聯系方式,我的QQ 285678313

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2014-04-17 13:39 by 黑色柳丁
          一模一樣的配置,運行后提示:No ASM ClassWriterFound

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2014-04-17 13:39 by 黑色柳丁
          然后引入asm的jar包就ok了。

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2014-09-24 11:22 by 555
          555

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2014-09-25 14:55 by 鄧豐坡
          再次感謝,之前找了N多資料,也沒有你的一個實例明了。
          不過你的jar少說了
          stax2-api-3.1.1.jar
          woodstox-core-asl-4.2.0.jar
          這個確實

          # re: CXF+Spring+Tomcat簡明示例[未登錄]  回復  更多評論   

          2014-10-10 11:08 by william
          非常感謝樓主的分享,實例太棒啦,我了兩天,這是最棒的

          # re: CXF+Spring+Tomcat簡明示例[未登錄]  回復  更多評論   

          2014-11-10 13:30 by ada
          謝謝樓主,困擾我兩天的問題終于解決了!

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2014-11-29 10:51 by
          @求知者 QQ 285678313
          我也遇到了同樣的問題,請問怎么解決。謝謝

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2015-09-19 19:31 by 枯荷聽雨
          怎樣調用那個接口,客戶端如何訪問呢?

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2015-12-17 14:20 by 匿名者
          @林祥
          在cxf.jar中

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2016-07-16 17:44 by chiangpan
          CXF版本不同,有些其實不需要<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />這些配置

          # re: CXF+Spring+Tomcat簡明示例  回復  更多評論   

          2016-07-23 15:01 by 無異
          請問有沒有遇到這個問題的?

          org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'wbService': Invocation of init method failed; nested exception is java.lang.VerifyError: (class: com/wfy/service/jaxws_asm/Aaaa, method: setUserName signature: (Ljava/lang/String;)V) Illegal instruction found at offset 1
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1507)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
          at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:299)
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:295)
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:644)
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:493)
          at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)
          at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)
          at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
          at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4887)
          at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5381)
          at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
          at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
          at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
          at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:633)
          at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1113)
          at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1671)
          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
          at java.util.concurrent.FutureTask.run(FutureTask.java:262)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
          主站蜘蛛池模板: 河间市| 怀宁县| 宜兰市| 宜良县| 洪雅县| 凯里市| 理塘县| 江津市| 明光市| 泗阳县| 古丈县| 盖州市| 潼关县| 上杭县| 陕西省| 金坛市| 比如县| 神池县| 荣成市| 库尔勒市| 胶州市| 宁南县| 比如县| 伊宁县| 鞍山市| 集贤县| 金寨县| 垦利县| 新丰县| 宜都市| 惠来县| 重庆市| 清远市| 宁国市| 汾阳市| 合水县| 浦北县| 伊春市| 贡嘎县| 大田县| 玉溪市|