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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

cxf-spring-pratice-service

發布時間:2024/1/1 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 cxf-spring-pratice-service 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

其實吧,這篇博客基本上就是照搬了官方的How-to.

2017/11/15 時隔六個月, 重新下組織格式和內容.

諸如webservice,WSDL的定義及作用,還有意義啥的咱就不再廢話了,直接上干貨。

1. 引入cxf相關的依賴

1.1 使用maven
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.1</version> </dependency> <dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.1</version> </dependency>

1.2 直接引入相關jar

真心不建議采用這種方式!

asm-5.0.4.jar cxf-core-3.1.1.jar cxf-rt-bindings-soap-3.1.1.jar cxf-rt-bindings-xml-3.1.1.jar cxf-rt-databinding-jaxb-3.1.1.jar cxf-rt-frontend-jaxws-3.1.1.jar cxf-rt-frontend-simple-3.1.1.jar cxf-rt-transports-http-3.1.1.jar cxf-rt-ws-addr-3.1.1.jar cxf-rt-ws-policy-3.1.1.jar cxf-rt-wsdl-3.1.1.jar jaxb-core-2.2.11.jar jaxb-impl-2.2.11.jar neethi-3.0.3.jar stax2-api-3.1.4.jar woodstox-core-asl-4.4.1.jar wsdl4j-1.6.3.jar xml-resolver-1.2.jar xmlschema-core-2.2.1.jar

2. 創建相關類

2.1 IHelloWorld接口

@WebService interface IHelloWorld {String sayHello(@WebParam(name = "username") String username);Map<String, Object> getObj(@WebParam(name = "username")String username, @WebParam(name = "sexy")String sexy); }

2.2 HelloWorldImpl實現類

public class HelloWorldImpl implements IHelloWorld { @Override public String sayHello(String username) { return "Hello " + username; } @Overridepublic Map<String, Object> getObj(String username, String sexy) {Map<String, Object> customer = new HashMap<String, Object>();customer.put("NAME", username);customer.put("SEXY", sexy);customer.put("BIRTHDAY", Calendar.getInstance().getTime());return customer;} }

3. Spring配置

spring-cxf-service.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:http-conf="http://cxf.apache.org/transports/http/configuration"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 http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"><!-- 以下這個文件位于cxf-core-3.1.1.jar中 --><import resource="classpath:META-INF/cxf/cxf.xml" /><!-- 以下這個文件位于cxf-rt-databinding-jaxb-3.1.1.jar中 --><import resource="classpath:META-INF/cxf/cxf-servlet.xml" /><jaxws:endpoint id="helloService"implementor="com.kq.webservice.impl.HelloWorldImpl"address="/helloService" /> </beans>

4. web.xml

<!-- 載入上面配置的spring-cxf-service.xml文件 --> <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:config/spring.xml;<!-- cxf配置,必須在這里;這里涉及到的是SpringMVC中child,parent container的加載問題 -->classpath:config/spring-cxf-service.xml; </param-value> </context-param><!-- 配置cxf相關的servlet --> <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>/webservice/*</url-pattern> </servlet-mapping>

5. 訪問

  • 獲取該路徑所有的服務列表

    // 格式 http://host:port/context_path/services// sample http://localhost:9090/SpringMVCDemo/webservice
  • 讀取wsdl文件內容

    // 格式 http://host:port/context_path/services/serviceName?wsdl// sample http://localhost:9090/SpringMVCDemo/webservice/helloService?wsdl
  • 6. 測試

    6.1 單獨的測試項目

  • 引入相同的cxf相關依賴.
  • 唯一需要注意的是webservice接口名彼此必須一模一樣.
  • 6.2 基于SoapUI進行接口測試

  • 提取出指定的wsdl文件,按照上面的方式提取出指定的wsdl內容,復制到本地的文件中.
  • 使用提取到的wsdl文件創建soapUI工程.
  • 6.3 編寫測試類

    此種方式比較方便,無須配置比較靈活,大部分情況下都會采用這種方式調用服務程序。

  • 返回簡單的string

    private static void test_returnSimpleString(){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(IHelloWorld.class);factory.setAddress("http://localhost:8080/JYXT/webservice/helloService");IHelloWorld client = (IHelloWorld) factory.create();String response = client.sayHello("LQ");System.out.println("Response:" + response); }
  • 返回復雜對象

    private static void test_returnCustomObj(){JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();factory.setServiceClass(IHelloWorld.class);factory.setAddress("http://localhost:8080/JYXT/webservice/helloService");IHelloWorld client = (IHelloWorld) factory.create();Map<String, Object> obj = client.getObj("LQ","1");System.out.println(obj); }
  • 返回JSON

    未完成
  • 7. 疑難問題

  • Exception in thread "main" **org.apache.cxf.common.i18n.UncheckedException**: No operation was found with the name {http://impl.server.test.com/}helloWorld.

  • 參考鏈接 : https://www.cnblogs.com/yshyee/p/3633537.html
  • 解決方法:對服務端的接口實現類中的@WebService添加targetNamespace,其值為接口包名的倒置。例如我的IHelloWorld接口所在的包為com.test.server,此時對應的targeNamespace的值為http://server.test.com/

    @WebService(endpointInterface = "com.test.server.IHelloWorld", serviceName="helloWorld", targetNamespace="http://server.test.com/") public class HelloWorldImp implements IHelloWorld {public String helloWorld(String name) {return name+" Hello,World!";}}
  • 8. 參考鏈接

  • http://cxf.apache.org/docs/writing-a-service-with-spring.html - 官方文檔

  • http://blog.csdn.net/blueheart20/article/details/42971713

  • 有幾處問題.基本是因為cxf的版本問題導致的。
  • 關于webservice相關的理論可以在此文章中看看。
  • 總結

    以上是生活随笔為你收集整理的cxf-spring-pratice-service的全部內容,希望文章能夠幫你解決所遇到的問題。

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