java調用webservice接口 三種方法,基于SpringBoot 2.3的WebService指南(包含案例)

 2023-10-13 阅读 34 评论 0

摘要:基于SpringBoot 2.3的WebService指南 文章目錄基于SpringBoot 2.3的WebService指南前言一、WSDL解析和使用1.引入文檔的wsdl2.使用CXF解析WSDL (客戶端)2.Restful風格服務端二、BUG解決1.提示 “The Bean Validation API is on the classpath but no implementa

基于SpringBoot 2.3的WebService指南


文章目錄

  • 基于SpringBoot 2.3的WebService指南
  • 前言
  • 一、WSDL解析和使用
    • 1.引入文檔的wsdl
    • 2.使用CXF解析WSDL (客戶端)
    • 2.Restful風格服務端
  • 二、BUG解決
    • 1.提示 “The Bean Validation API is on the classpath but no implementation could be found”
    • 2.提示 “Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration ”
  • 三、案例下載


前言

提示:先看Spring版的教程,默認有Spring版基礎

  <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.3.RELEASE</version><relativePath/></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency><!--CXF做 WebService--><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxrs</artifactId><version>3.4.0</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.4.0</version></dependency><!--引入mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.38</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.19</version></dependency><!--工具類--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.11</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.5.1</version></dependency><!--JSON依賴--><dependency><groupId>org.json</groupId><artifactId>json</artifactId><version>20180813</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.73</version></dependency><!--緩存--><dependency><groupId>net.jodah</groupId><artifactId>expiringmap</artifactId><version>0.5.8</version></dependency><!--雜項--><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.2</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.2</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.9.0</version></dependency><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

一、WSDL解析和使用

跨平臺數據交互需要使用 WSDL ,例如A平臺使用 .NET開發,B平臺使用 Java開發,雙方需要對接,因此需要使用 WSDL。無論是 SOAP 的還是 Restful 風格,webservice的效率都要比同平臺直接對接效率低

1.引入文檔的wsdl

開發文檔中會包含WSDL鏈接,想要看懂wsdl實際上沒有必要,如果有需要可以查看

https://www.cnblogs.com/wangsen/p/5777541.html

java調用webservice接口 三種方法、如果沒有 WSDL地址,可以參考這篇文章:

https://wangmaoxiong.blog.csdn.net/article/details/87370571

2.使用CXF解析WSDL (客戶端)

  1. 從 http://cxf.apache.org/download.html 下載CXF包,解壓并進行環境變量配置(參考JDK環境變量配置)
  2. 使用cmder運行 “ wsimport -s . -p 文件夾名稱 項目wsdl地址 ” 生成wsdl反編譯代碼
    【例如 wsimport -s . -p com.dmbjz.client http://localhost:8888/weather?wsdl,反編譯后會在當前文件夾下創建com/dmbjz/client文件夾,反編譯的代碼就在里面,拷貝到項目中后。反編譯代碼也必須在java/com/dmbjz/client】
  3. 刪除步驟2的class文件,然后將生成代碼拷貝到項目中
  4. 傳統是使用JDK反射創建Service或者傳統視圖方式進行請求,但是這兩個方法都在傳遞String時需要指定格式編碼,略微復雜,但采用CXF框架只需要這幾步,一般寫在項目的Service:
 //省略項目本身的service實現邏輯,需要調用第三方服務的時候寫下面的代碼String address = "http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl";//文檔里的WSDL地址JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();factoryBean.setAddress(address);      // 設置代理地址factoryBean.setServiceClass(IpAddressSearchWebServiceSoap.class);    // 設置接口類型,直白點就是填上反編譯出來的需要調用的接口IpAddressSearchWebServiceSoap userWebService = (IpAddressSearchWebServiceSoap) factoryBean.create();// 創建一個代理接口實現ArrayOfString geoIPContext = userWebService.getGeoIPContext();//獲取接口里你需要使用的方法System.out.println(geoIPContext);
  1. 結束

2.Restful風格服務端

  1. 把需要給第三方調用的接口和實現類復制一份出來改個名字,比如原接口叫UserSertvice,實現類叫UserServiuceImpl,復制完叫UserWebService和UserWebServiuceImpl
  2. 在接口和實現類上添加@WebService,實現類上加上@Component
  3. 在實現類上導入原接口,定義請求方法、請求路徑等
* @Path:作用在方法上,代表當前服務方法的訪問路徑
* @GET/@DELETE/@PUT/@POST:   (請求方式->查、刪、更新、插入)
* @PathParam:   作用在參數上,相當于MVC的@PathVariable (書寫路徑參數的別名)
* @Produces:    作用在方法上,控制請求的返回類型
  1. 在配置文件進行服務發布
@Configuration
public class CXFConfig {@Autowiredprivate Bus bus;@Autowiredprivate UserWebService userWebService;/*相當于以前的 Web.xml代理 Webservice服務*/@Beanpublic ServletRegistrationBean cxfservlet(){return new ServletRegistrationBean(new CXFServlet(),"/cxf/*");}/*服務注冊*/@Beanpublic Endpoint userEndpoint(){EndpointImpl endpoint = new EndpointImpl(bus,userWebService);endpoint.publish("/wb");return endpoint;}
}
  1. 運行項目,直接訪問,該項目就是 http://localhost:8080/cxf/wb?wsdl

二、BUG解決

1.提示 “The Bean Validation API is on the classpath but no implementation could be found”

***************************APPLICATION FAILED TO START***************************Description:The Bean Validation API is on the classpath but no implementation could be foundAction:Add an implementation, such as Hibernate Validator, to the classpathProcess finished with exit code 1

原因是SpringBoot版本和 CXF版本不匹配,SpringBoot2.3以上需要使用3.4.0+版本的CXF依賴

2.提示 “Parameter 0 of method errorPageCustomizer in ErrorMvcAutoConfiguration ”

***************************
APPLICATION FAILED TO START
***************************Description:Parameter 0 of method errorPageCustomizer in org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration required a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' that could not be found.The following candidates were found but could not be injected:- Bean method 'dispatcherServletRegistration' in 'DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration' not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServletAction:Consider revisiting the entries above or defining a bean of type 'org.springframework.boot.autoconfigure.web.servlet.DispatcherServletPath' in your configuration.Process finished with exit code 1

原因是在配置文件中編寫代理 Webservice服務的時候,名稱沖突,改一下圖下方法名即可解決

@Beanpublic ServletRegistrationBean 改成其他方法名(){return new ServletRegistrationBean(new CXFServlet(),"/cxf/*");}

三、案例下載

包含Spring版本的Servie + Client,SpringBoot的 Client、直接調用的Service和Client

https://gitee.com/dmbjzorg/Webservice.git

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/135737.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息