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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

WebService学习笔记---CXF入门

發(fā)布時(shí)間:2023/12/4 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WebService学习笔记---CXF入门 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

一、準(zhǔn)備

軟件環(huán)境:

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

二、創(chuàng)建項(xiàng)目

  • 新建一個(gè)Maven項(xiàng)目,在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官網(wǎng)下載已編譯的文件,通過(guò)BuildPath添加
  • <dependency><groupId>org.apache.cxf</groupId><artifactId>apache-cxf</artifactId><version>3.1.5</version><type>zip</type> //因?yàn)镸aven遠(yuǎn)程倉(cāng)庫(kù)只有zip,tar.gz等壓縮包,沒(méi)有對(duì)應(yīng)jar包,所有要指定type </dependency>

    用過(guò)BuildPath添加 右鍵項(xiàng)目---》 Build Path---》 Configure Build Path---》 Add Library---》 在對(duì)話框中選擇CXF Runtime---》 如果是第一自使用CXF,則需要配置CXF,點(diǎn)擊Configure installed runtimes,新增一個(gè)CXF---》 添加成功,在Libraries里會(huì)多出一個(gè)Apache CXF Libary[3.1.5]

    三、編寫(xiě)類(lèi)文件

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

    說(shuō)明: @WebService:標(biāo)記表示該接口是一個(gè)WebService服務(wù)。 @WebMethod:標(biāo)記表示W(wǎng)ebService中的方法。 @WebParam(name="paramName")表示方法中的參數(shù),name屬性限制了參數(shù)的名稱,若沒(méi)有指定該屬性,參數(shù)將會(huì)被重命名。

  • 新建一個(gè)實(shí)現(xiàn)類(lèi)
  • 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>
  • 編寫(xiě)測(cè)試
  • 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);} }

    四、遇到的問(wèn)題

    Eclipse在創(chuàng)建Maven的時(shí)候會(huì)創(chuàng)建index.jsp文件。而pom.xml文件里只有junit依賴,所有還需手動(dòng)添加servlet-api

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

    還有一個(gè)問(wèn)題如下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>

    如果還不行,右鍵項(xiàng)目,點(diǎn)擊Properties,在Java Compiler里選擇Compiler Level為1.8

    項(xiàng)目報(bào)錯(cuò)提示沒(méi)有src\main\java,src\test\resources,src\test\java等文件夾

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

    轉(zhuǎn)載于:https://my.oschina.net/yuewawa/blog/649150

    總結(jié)

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

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。