日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

WebService学习笔记---CXF入门

發布時間:2023/12/4 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WebService学习笔记---CXF入门 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

一、準備

軟件環境:

JDK1.8, Eclipse JEE 4.4, Maven-3.2.5, Spring-4, CXF-3.1.5

二、創建項目

  • 新建一個Maven項目,在pom.xml里添加spring依賴
  • <dependencyManagement><dependencies><dependency><groupId>io.spring.platform</groupId><artifactId>platform-bom</artifactId><version>1.1.3.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement>
  • 添加cxf依賴,或是從apache cxf官網下載已編譯的文件,通過BuildPath添加
  • <dependency><groupId>org.apache.cxf</groupId><artifactId>apache-cxf</artifactId><version>3.1.5</version><type>zip</type> //因為Maven遠程倉庫只有zip,tar.gz等壓縮包,沒有對應jar包,所有要指定type </dependency>

    用過BuildPath添加 右鍵項目---》 Build Path---》 Configure Build Path---》 Add Library---》 在對話框中選擇CXF Runtime---》 如果是第一自使用CXF,則需要配置CXF,點擊Configure installed runtimes,新增一個CXF---》 添加成功,在Libraries里會多出一個Apache CXF Libary[3.1.5]

    三、編寫類文件

  • 新建一個接口類
  • @WebService public interface ICXFService{@WebMethodpublic String sayHello(@WebParam(name="username") String username); }

    說明: @WebService:標記表示該接口是一個WebService服務。 @WebMethod:標記表示WebService中的方法。 @WebParam(name="paramName")表示方法中的參數,name屬性限制了參數的名稱,若沒有指定該屬性,參數將會被重命名。

  • 新建一個實現類
  • public class CXFService implements ICXFService{public String sayHello(String username){return "Hello, "+username; } }
  • 在WEB-INF下新建cxf-servlet.xml
  • <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- START SNIPPET: beans --> <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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"><import resource="classpath:META-INF/cxf/cxf.xml" /><jaxws:endpoint id="sayHello" implementor="com.demo.cxfws.service.impl.CXFServiceImpl" address="/sayHello" /> </beans> <!-- END SNIPPET: beans -->
  • 在web.xml中添加cxf配置信息
  • <servlet><servlet-name>cxfServlet</servlet-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>/*</url-pattern> </servlet-mapping><session-config><session-timeout>60</session-timeout> </session-config>
  • 在src\main\resources下新建client-beans.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://cxf.apache.org/jaxwshttp://cxf.apache.org/schema/jaxws.xsd"> <bean id="client" class="com.demo.cxfws.service.ICXFService" factory-bean="clientFactory" factory-method="create" /><bean id="clientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"><property name="serviceClass" value="com.demo.cxfws.service.ICXFService" /><property name="address" value="http://localhost:8080/cxfws/sayHello" /></bean> </beans>
  • 編寫測試
  • public class TestCXF {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"client-beans.xml"});ICXFService service = (ICXFService) context.getBean("client");String str = service.sayHello("zhangsan");System.out.println(str);} }

    四、遇到的問題

    Eclipse在創建Maven的時候會創建index.jsp文件。而pom.xml文件里只有junit依賴,所有還需手動添加servlet-api

    <dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version> </dependency>

    還有一個問題如下Eclipse JAX-RS (REST Web Services) 2.0 requires Java 1.6 or newer

    如果遇到了,在pom.xml添加:

    <build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins> </build>

    如果還不行,右鍵項目,點擊Properties,在Java Compiler里選擇Compiler Level為1.8

    項目報錯提示沒有src\main\java,src\test\resources,src\test\java等文件夾

    解決辦法:進入Java Build Path下的Source選卡,移除打小紅叉的文件夾,然后自己右鍵項目新建Source Folder。

    轉載于:https://my.oschina.net/yuewawa/blog/649150

    總結

    以上是生活随笔為你收集整理的WebService学习笔记---CXF入门的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。