Spring boot webService使用方法解析
以前一家公司,項(xiàng)目用到webservice,不過(guò)后來(lái)沒(méi)待多久,當(dāng)時(shí)也要弄?jiǎng)e的也就沒(méi)有研究,
這次也遇到過(guò)這樣一個(gè)使用場(chǎng)景,需要對(duì)接別人的一個(gè)人臉識(shí)別服務(wù),在什么都沒(méi)有的情況下,對(duì)方只給了一個(gè)wsdl的地址過(guò)來(lái),全程都靠自己去研究了.
先就webservice 講下自己的理解把,感覺(jué)有點(diǎn)像websockt ,它可以實(shí)現(xiàn)一個(gè)服務(wù)端, 然后在客戶(hù)端去調(diào)用服務(wù)端去完成服務(wù)端的操作.
這里使用spring-boot
1.先創(chuàng)建spring-boot項(xiàng)目,引入jar包
2.創(chuàng)建一個(gè)對(duì)象.
<!-- web Services --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-spring-boot-starter-jaxws</artifactId> <version>3.2.7</version> </dependency>
創(chuàng)建一個(gè)服務(wù)端接口
package com.sunzy.mywebservice; import lombok.Getter; import lombok.Setter; /**auth : szy *time : 2020-01-03 **/ @Getter @Setter public class Person { private Integer id; private String name; private String niceName; private Integer age; private Double height; }
服務(wù)端的實(shí)現(xiàn)方法
package com.sunzy.mywebservice.service.Impl; import com.alibaba.fastjson.JSONArray; import com.sunzy.mywebservice.Person; import com.sunzy.mywebservice.service.TestApiService; import org.springframework.stereotype.Component; import javax.jws.WebService; import java.util.List; /**auth : szy *time : 2020-01-03 **/ @Component @WebService(name = "testApiService", targetNamespace = "http://service.mywebservice.sunzy.com", endpointInterface = "com.sunzy.mywebservice.service.TestApiService", portName = "10000") public class TestApiServiceImpl implements TestApiService { @Override public Person insertPersonInfo(String person) { System.out.println("服務(wù)端接口到了請(qǐng)求:person="+person); List<Person> list = JSONArray.parseArray(person, Person.class); //TODO 邏輯處理 return list.get(0); } }
配置文件,將服務(wù)進(jìn)行開(kāi)放出去,給外部使用.
到這里已經(jīng)完成了,運(yùn)行項(xiàng)目.訪(fǎng)問(wèn)地址:http://localhost:8080/ws/testApiService?wsdl
能輸出下面,表示服務(wù)端部署成功了.
那么下面是客戶(hù)端如何去訪(fǎng)問(wèn)
可以新建一個(gè)項(xiàng)目,這里采用本項(xiàng)目去調(diào)用.用idea 去解析wsdl,生成對(duì)應(yīng)的代碼.
選擇項(xiàng)目
通過(guò)wsdl,生成java代碼.
上面填生成的地址,下面試填寫(xiě)包名,重點(diǎn) 這里上面的地址是要有效能訪(fǎng)問(wèn)到的,不然程序是讀取不到東西的,更不要說(shuō)解析了
生成代碼完成后,可以看到代碼:
調(diào)用方法,這里就自己寫(xiě)一個(gè)控制器,模仿下客戶(hù)端去調(diào)用.
package com.sunzy.mywebservice.controller;/** * @title: Hello * @projectName mywebservice * @description: TODO * @author :szy * @date 2020/1/3-15:44 */ import com.sunzy.mywebservice.Person; import com.sunzy.mywebservice.config.TestApiServiceImplService; import com.sunzy.mywebservice.service.TestApiService; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.net.URL; import java.util.Map; /**auth : szy *time : 2020-01-03 **/ @RestController @RequestMapping("/adminWebservice") public class Hello { // 獲取單位信息 @GetMapping(value="/sync") public String sync(@RequestParam(value="data") String data) throws Exception{ // 接口地址 String address = "http://localhost:8080/ws/testApiService?wsdl"; // 代理工廠(chǎng) JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 設(shè)置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 設(shè)置接口類(lèi)型 jaxWsProxyFactoryBean.setServiceClass(TestApiService.class); // 創(chuàng)建一個(gè)代理接口實(shí)現(xiàn) TestApiService us = (TestApiService) jaxWsProxyFactoryBean.create(); // 數(shù)據(jù)準(zhǔn)備 String userId = "[{\"name\":\"JACK\"},{\"name\":\"TOM\"}]"; // 調(diào)用代理接口的方法調(diào)用并返回結(jié)果 Person person = us.insertPersonInfo(userId); System.out.println("返回結(jié)果:" + person.toString()); return "index"; } // 動(dòng)態(tài)調(diào)用 外部調(diào)用 @GetMapping(value="/dontest") public String dontest(@RequestParam(value="data") String data) throws Exception{ JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient("http://127.0.0.1:8080/ws/testApiService?wsdl"); Object[] objects = new Object[0]; try { // invoke("方法名",參數(shù)1,參數(shù)2,參數(shù)3....); // 數(shù)據(jù)準(zhǔn)備 String userId = "[{\"name\":\"JACK\"},{\"name\":\"TOM\"}]"; objects = client.invoke("insertPersonInfo", userId); System.out.println("返回?cái)?shù)據(jù):" + objects[0]); } catch (java.lang.Exception e) { e.printStackTrace(); } return "index"; } // 動(dòng)態(tài)調(diào)用 外部調(diào)用(外部模擬客服端調(diào)用服務(wù)端) @GetMapping(value="/dontest2") public String dontest2(@RequestParam(value="data") String data) throws Exception{ //調(diào)用服務(wù)端 TestApiServiceImplService serviceImplService = new TestApiServiceImplService(); com.sunzy.mywebservice.config.TestApiService apiService = serviceImplService.get10000(); String userId = "[{\"name\":\"小紅\"},{\"name\":\"小藍(lán)\"}]"; com.sunzy.mywebservice.config.Person x = apiService.insertPersonInfo(userId); //服務(wù)端返回的數(shù)據(jù) System.out.println("返回?cái)?shù)據(jù):" + x.toString()); return "index"; } }
通過(guò)postman可以看到調(diào)用成功.
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot調(diào)用第三方WebService接口的操作技巧(.wsdl與.asmx類(lèi)型)
- SpringBoot整合WebService的實(shí)現(xiàn)示例
- SpringBoot創(chuàng)建WebService方法詳解
- SpringBoot調(diào)用第三方WebService接口的兩種方法
- SpringBoot調(diào)用對(duì)方webService接口的幾種方法示例
- SpringBoot整合WebService的實(shí)戰(zhàn)案例
- springboot整合webservice使用簡(jiǎn)單案例總結(jié)
- Java(Springboot)項(xiàng)目調(diào)用第三方WebService接口實(shí)現(xiàn)代碼
相關(guān)文章
利用AOP實(shí)現(xiàn)系統(tǒng)告警的方法詳解
在開(kāi)發(fā)的過(guò)程中會(huì)遇到各種各樣的開(kāi)發(fā)問(wèn)題,服務(wù)器宕機(jī)、網(wǎng)絡(luò)抖動(dòng)、代碼本身的bug等等。針對(duì)代碼的bug,我們可以提前預(yù)支,通過(guò)發(fā)送告警信息來(lái)警示我們?nèi)ジ深A(yù),盡早處理。本文將利用AOP實(shí)現(xiàn)系統(tǒng)告警,需要的可以參考一下2022-09-09SpringBoot集成阿里巴巴Druid監(jiān)控的示例代碼
這篇文章主要介紹了SpringBoot集成阿里巴巴Druid監(jiān)控的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04java 中類(lèi)似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例
這篇文章主要介紹了java 中類(lèi)似js encodeURIComponent 函數(shù)的實(shí)現(xiàn)案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10mybatis-plus查詢(xún)無(wú)數(shù)據(jù)問(wèn)題及解決
這篇文章主要介紹了mybatis-plus查詢(xún)無(wú)數(shù)據(jù)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12springboot項(xiàng)目開(kāi)啟https協(xié)議的項(xiàng)目實(shí)現(xiàn)
本文主要介紹了springboot項(xiàng)目開(kāi)啟https協(xié)議的項(xiàng)目實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07詳解Java中finally和return的執(zhí)行順序
try-catch-finally是一種針對(duì)程序運(yùn)行時(shí)出錯(cuò)的響應(yīng)手段,對(duì)于一些可以預(yù)料到的出錯(cuò)類(lèi)型,在發(fā)生時(shí)對(duì)其進(jìn)行報(bào)告和補(bǔ)救,這篇文章主要介紹了Java中finally和return的執(zhí)行順序,需要的朋友可以參考下2024-01-01Java編程反射機(jī)制用法入門(mén)與實(shí)例總結(jié)
這篇文章主要介紹了Java編程反射機(jī)制用法,簡(jiǎn)單說(shuō)明了反射機(jī)制的概念、原理并結(jié)合實(shí)例形式總結(jié)分析了java反射機(jī)制的簡(jiǎn)單使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-12-12java web實(shí)現(xiàn)郵箱激活與忘記密碼
這篇文章主要為大家詳細(xì)介紹了java web實(shí)現(xiàn)郵箱激活與忘記密碼、重置密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02