成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Eclipse+Maven+Spring+CXF 構(gòu)建webservice 服務(wù)

云計(jì)算
本文通過(guò)實(shí)際案例詳細(xì)介紹如何通過(guò)Eclipse+Maven+Spring+CXF來(lái)構(gòu)建webservice 服務(wù) 。

一、 軟件準(zhǔn)備

  • Eclipse 4.2.1
  • Maven 2.2.1
  • Spring 3.2.6
  • CXF 3.0.2

二、 步驟

1.        新建web工程,利用maven管理,如下:

  

工程名為test,完成以后,項(xiàng)目結(jié)構(gòu)如下圖:

src/main/java 準(zhǔn)備放 java 程序;

src/main/resources準(zhǔn)備放各類資源文件。

2.  添加代碼

1)        定義服務(wù)接口

  1. ackage com.test;   
  2.     
  3. import javax.jws.WebService;   
  4.     
  5. @WebService   
  6. public interface HelloWorld {   
  7.     public String sayHello();   
  8. }   

因?yàn)橹皇且粋€(gè)webservice的實(shí)驗(yàn)程序,所以非常簡(jiǎn)單,只有一個(gè)服務(wù)方法: sayHello(),利用 @WebService注解來(lái)聲明這是一個(gè)webservice的接口。

2)        實(shí)現(xiàn)服務(wù)類

  1. package com.test;   
  2.     
  3. import javax.jws.WebService;   
  4.     
  5. @WebService   
  6. public class HelloWorldImpl implements HelloWorld{   
  7.       
  8.     public String sayHello(){   
  9.         return "Hello world!";   
  10.     }   
  11. }   

完成java代碼添加后的項(xiàng)目結(jié)構(gòu)如下:

3.        添加Spring-CXF 配置

在項(xiàng)目 src/main/webapp/WEB-INF 目錄下新建XML定義:cxf-servlet.xml如下:

  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!--   
  3.         Licensed to the Apache Software Foundation (ASF) under one   
  4.         or more contributor license agreements. See the NOTICE file   
  5.         distributed with this work for additional information   
  6.         regarding copyright ownership. The ASF licenses this file   
  7.         to you under the Apache License, Version 2.0 (the   
  8.         "License"); you may not use this file except in compliance   
  9.         with the License. You may obtain a copy of the License at   
  10.           
  11.         http://www.apache.org/licenses/LICENSE-2.0   
  12.           
  13.         Unless required by applicable law or agreed to in writing,   
  14.         software distributed under the License is distributed on an   
  15.         "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY   
  16.         KIND, either express or implied. See the License for the   
  17.         specific language governing permissions and limitations   
  18.         under the License.   
  19. -->   
  20. <!-- START SNIPPET: beans -->   
  21. <beans xmlns="http://www.springframework.org/schema/beans"   
  22.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  23.     xmlns:jaxws="http://cxf.apache.org/jaxws"   
  24.     xsi:schemaLocation=" http://www.springframework.org/schema/beans   
  25.     http://www.springframework.org/schema/beans/spring-beans.xsd   
  26.     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">   
  27.     <import resource="classpath:META-INF/cxf/cxf.xml"/>   
  28.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>   
  29.     <jaxws:endpoint id="helloWorld" implementor="com.test.HelloWorldImpl" address="/HelloWorld"/>   
  30. </beans>   
  31. <!-- END SNIPPET: beans -->   

該定義文件利用spring和CXF的功能,發(fā)布一個(gè)ID為helloWorld,實(shí)現(xiàn)類為com.test.HelloWorldImpl,發(fā)布 相對(duì)路徑為 /HelloWorld(對(duì)應(yīng)絕對(duì)目錄為: http://host:port/{WebAPPName}/HelloWorld)的 webservice。 因?yàn)槲覀冃枰玫紺XF來(lái)做webservice,右鍵點(diǎn)擊項(xiàng)目中的POM.XML,添加apache-cxf依賴。

4.  Web應(yīng)用配置

修改 src/main/webapp/WEB-INF 目錄下的web.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!--   
  3.   Licensed to the Apache Software Foundation (ASF) under one   
  4.   or more contributor license agreements. See the NOTICE file   
  5.   distributed with this work for additional information   
  6.   regarding copyright ownership. The ASF licenses this file   
  7.   to you under the Apache License, Version 2.0 (the   
  8.   "License"); you may not use this file except in compliance   
  9.   with the License. You may obtain a copy of the License at   
  10.     
  11.   http://www.apache.org/licenses/LICENSE-2.0   
  12.     
  13.   Unless required by applicable law or agreed to in writing,   
  14.   software distributed under the License is distributed on an   
  15.   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY   
  16.   KIND, either express or implied. See the License for the   
  17.   specific language governing permissions and limitations   
  18.   under the License.   
  19. -->   
  20. <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee           http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">   
  21.     <display-name>cxf</display-name>   
  22.     <servlet>   
  23.         <description>Apache CXF Endpoint</description>   
  24.         <display-name>cxf</display-name>   
  25.         <servlet-name>cxf</servlet-name>   
  26.         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>   
  27.         <load-on-startup>1</load-on-startup>   
  28.     </servlet>   
  29.     <servlet-mapping>   
  30.         <servlet-name>cxf</servlet-name>   
  31.         <url-pattern>/*</url-pattern>   
  32.     </servlet-mapping>   
  33.     <session-config>   
  34.         <session-timeout>60</session-timeout>   
  35.     </session-config>   
  36. </web-app>   

該文件實(shí)際上是定義了處理webservice的CXF Servlet的映射關(guān)系。

 完成步驟3和4以后的工程目錄如下:

5.  編譯打包

利用maven(package -X)編譯打包成test.war

(在Eclipse上右擊工程名 Run as -> Maven build)

6.  將步驟5生成的test.war部署到tomcat服務(wù)器上

7.  訪問(wèn)測(cè)試:

在瀏覽器上輸入:http://localhost:8080/test/,出現(xiàn)如下畫面就成功了:

點(diǎn)擊WSDL鏈接:

 

8.        編寫webservice client端代碼

1)        首先通過(guò) Spring 與 CXF 的配置來(lái)定義 Web Service 的客戶端 Bean,在 src\main\resources 目錄下創(chuàng)建client-beans.xml 配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>   
  2. <!--   
  3.         Licensed to the Apache Software Foundation (ASF) under one   
  4.         or more contributor license agreements. See the NOTICE file   
  5.         distributed with this work for additional information   
  6.         regarding copyright ownership. The ASF licenses this file   
  7.         to you under the Apache License, Version 2.0 (the   
  8.         "License"); you may not use this file except in compliance   
  9.         with the License. You may obtain a copy of the License at   
  10.           
  11.         http://www.apache.org/licenses/LICENSE-2.0   
  12.           
  13.         Unless required by applicable law or agreed to in writing,   
  14.         software distributed under the License is distributed on an   
  15.         "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY   
  16.         KIND, either express or implied. See the License for the   
  17.         specific language governing permissions and limitations   
  18.         under the License.   
  19. -->   
  20. <!-- START SNIPPET: beans -->   
  21. <beans xmlns="http://www.springframework.org/schema/beans"   
  22.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  23.     xmlns:jaxws="http://cxf.apache.org/jaxws"   
  24.     xsi:schemaLocation="   
  25.     http://www.springframework.org/schema/beans   
  26.     http://www.springframework.org/schema/beans/spring-beans.xsd   
  27.     http://cxf.apache.org/jaxws   
  28.      http://cxf.apache.org/schema/jaxws.xsd">   
  29.     <bean id="client" class="com.test.HelloWorld"   
  30.         factory-bean="clientFactory" factory-method="create"/>   
  31.     <bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean">   
  32.         <property name="serviceClass" value="com.test.HelloWorld"/>   
  33.         <property name="address" value="http://localhost:8080/test/HelloWorld"/>   
  34.     </bean>   
  35. </beans>   
  36. <!-- END SNIPPET: beans -->   

需要注意的是,該配置文件中的 address需要寫成發(fā)布服務(wù)的絕對(duì)路徑。

2) 編寫客戶端java代碼: HelloWorldClient.java

  1. package com.test;   
  2.     
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;   
  4.     
  5. public final class HelloWorldClient {   
  6.     
  7.     private HelloWorldClient() {   
  8.     }   
  9.     
  10.     public static void main(String args[]) throws Exception {   
  11.         // START SNIPPET: client   
  12.         ClassPathXmlApplicationContext context   
  13.             = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});   
  14.     
  15.         HelloWorld client = (HelloWorld)context.getBean("client");   
  16.     
  17.         String response = client.sayHello();   
  18.         System.out.println("Response: " + response);   
  19.         System.exit(0);   
  20.         // END SNIPPET: client   
  21.     }   
  22. }   

注意,代碼中HelloWorldclient = (HelloWorld)context.getBean("client"); 的client需要與"client-beans.xml"中的 bean id一致才能找到這個(gè)服務(wù)。 

現(xiàn)在的項(xiàng)目結(jié)構(gòu)如下:

3)連接測(cè)試

在eclipse中直接按HelloWorldClient運(yùn)行 Run as -> Java Application: 

 輸出的Hello world! 即是我們發(fā)布的HelloWorld的方法 sayHello()的輸出!這說(shuō)明從服務(wù)發(fā)布到客戶端連接都成功了。

原文出自:http://blog.csdn.net/hbsong75/article/details/41207585

責(zé)任編輯:Ophira 來(lái)源: hbsong75的專欄
相關(guān)推薦

2009-08-13 15:16:00

Eclipse建立We

2012-05-03 11:43:32

ApacheCXFRESTful

2009-06-18 10:19:00

Spring集成XFiWebService

2017-06-26 09:06:10

Spring Clou微服務(wù)架構(gòu)

2012-05-03 11:30:04

ApacheCXFJava

2012-05-03 11:21:58

ApacheCXFJava

2017-09-04 16:15:44

服務(wù)網(wǎng)關(guān)架構(gòu)

2022-10-10 08:00:00

微服務(wù)Spring Boo容器

2017-07-03 09:50:07

Spring Clou微服務(wù)架構(gòu)

2017-08-10 11:15:05

Spring Clou微服務(wù)架構(gòu)

2017-08-09 15:50:47

Spring Clou微服務(wù)架構(gòu)

2012-05-15 13:40:44

JavaCXF.NET

2024-09-11 09:15:06

2009-07-14 13:14:13

2017-06-25 13:33:25

Spring Clou微服務(wù)架構(gòu)

2017-07-04 17:35:46

微服務(wù)架構(gòu)Spring Clou

2018-03-02 16:11:29

Spring Clou分布式服務(wù)跟蹤

2012-05-03 11:35:56

ApacheCXFJava

2009-06-15 16:35:44

Spring IDEEclipse插件

2022-03-18 09:00:00

開發(fā)Web服務(wù)應(yīng)用程序
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 一区二区在线免费播放 | 欧美日一区二区 | 成人精品视频在线观看 | 一区二区三区日本 | 91一区二区三区在线观看 | 午夜视频在线观看视频 | 久久久www成人免费精品张筱雨 | 久久中文字幕一区 | 亚洲精品久久久一区二区三区 | 欧美一级二级三级 | 亚洲综合小视频 | 网络毛片 | 国产玖玖 | 中文字幕成人 | 久久久久久久一区二区三区 | 久99久视频| 亚洲三区视频 | 亚洲精品高清视频在线观看 | 精品免费视频 | 亚洲va欧美va天堂v国产综合 | 波多野结衣一区二区 | 黄色网址免费在线观看 | 午夜亚洲 | 久操伊人 | 精品九九九 | 日韩欧美一区二区三区免费观看 | 久久久综合网 | 一级a性色生活片久久毛片 一级特黄a大片 | 国产精品影视 | 日韩在线观看一区二区三区 | 亚州影院 | 中国一级特黄真人毛片免费观看 | 午夜成人免费视频 | 国产高清免费 | 国产精品久久久久久久久久久久午夜片 | 国产精品久久久久久久毛片 | 91精品国产色综合久久 | 国产精品一区久久久久 | 天天弄| 国产黄色一级电影 | 欧美日韩一区二区在线播放 |