亚洲免费在线视频-亚洲啊v-久久免费精品视频-国产精品va-看片地址-成人在线视频网

您的位置:首頁技術(shù)文章
文章詳情頁

Spring boot webService使用方法解析

瀏覽:104日期:2023-08-12 17:19:45

以前一家公司,項(xiàng)目用到webservice,不過后來沒待多久,當(dāng)時也要弄別的也就沒有研究,

這次也遇到過這樣一個使用場景,需要對接別人的一個人臉識別服務(wù),在什么都沒有的情況下,對方只給了一個wsdl的地址過來,全程都靠自己去研究了.

先就webservice 講下自己的理解把,感覺有點(diǎn)像websockt ,它可以實(shí)現(xiàn)一個服務(wù)端, 然后在客戶端去調(diào)用服務(wù)端去完成服務(wù)端的操作.

這里使用spring-boot

1.先創(chuàng)建spring-boot項(xiàng)目,引入jar包

2.創(chuàng)建一個對象.

<!-- 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)建一個服務(wù)端接口

package com.sunzy.mywebservice;import lombok.Getter;import lombok.Setter;/**auth : szy *time : 2020-01-03 **/@Getter@Setterpublic 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ù)端接口到了請求:person='+person); List<Person> list = JSONArray.parseArray(person, Person.class); //TODO 邏輯處理 return list.get(0); }}

配置文件,將服務(wù)進(jìn)行開放出去,給外部使用.

到這里已經(jīng)完成了,運(yùn)行項(xiàng)目.訪問地址:http://localhost:8080/ws/testApiService?wsdl

Spring boot webService使用方法解析

能輸出下面,表示服務(wù)端部署成功了.

那么下面是客戶端如何去訪問

可以新建一個項(xiàng)目,這里采用本項(xiàng)目去調(diào)用.用idea 去解析wsdl,生成對應(yīng)的代碼.

選擇項(xiàng)目

Spring boot webService使用方法解析

通過wsdl,生成java代碼.

Spring boot webService使用方法解析

上面填生成的地址,下面試填寫包名,重點(diǎn) 這里上面的地址是要有效能訪問到的,不然程序是讀取不到東西的,更不要說解析了

生成代碼完成后,可以看到代碼:

Spring boot webService使用方法解析

調(diào)用方法,這里就自己寫一個控制器,模仿下客戶端去調(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'; // 代理工廠 JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean(); // 設(shè)置代理地址 jaxWsProxyFactoryBean.setAddress(address); // 設(shè)置接口類型 jaxWsProxyFactoryBean.setServiceClass(TestApiService.class); // 創(chuàng)建一個代理接口實(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'; } // 動態(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'; } // 動態(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'; }}

通過postman可以看到調(diào)用成功.

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美特黄一级高清免费的香蕉 | 欧美一级视频在线 | 久草一级片 | 天天五月天丁香婷婷深爱综合 | 欧美日韩亚洲高清不卡一区二区三区 | 国产成人系列 | 老司机午夜精品网站在线观看 | 国产高清专区 | 日本在线免费观看视频 | 欧美自拍在线 | 天天都色| 国产一级毛片在线 | 97视频免费播放观看在线视频 | 欧美三级aaa | 日本国产精品 | 日本一线一区二区三区免费视频 | 日韩一区二区在线免费观看 | 欧美区一区二 | 夜色www国产精品资源站 | 欧美日韩精品一区二区三区 | 一区二区三区在线 | 精品视频在线免费播放 | 久久99国产精品久久99果冻传媒 | 偷拍自拍第一页 | 欧美性性性性性色大片免费的 | 亚洲免费区| 久久精品久久精品久久精品 | 国内精品伊人久久久久妇 | 国内自拍视频在线看免费观看 | 久久综合九九亚洲一区 | 久久久精品国产 | 免费观看一级成人毛片软件 | 免费人成黄页网站在线观看 | videosfree性欧美另类 | 鲁丝一区二区三区不属 | a欧美在线| 视频久久精品 | 亚洲精品久久九九精品 | 久久亚洲国产中v天仙www | 娇小性色xxxxx中文 | 成人午夜影视全部免费看 |