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

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

生活随笔

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

编程问答

两个服务之间的调用请求

發(fā)布時(shí)間:2023/12/18 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 两个服务之间的调用请求 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

?

使用

package com.thunisoft.sspt.bootstrap;import com.thunisoft.maybee.engine.restclient.RestClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate;import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map;@Configuration public class LocalRestClient {@Autowiredprivate RestTemplate restTemplate;@Beanpublic RestClient restClient() {Map<String, String> server = new HashMap<>();server.put("scheme", "http");server.put("host", "localhost");server.put("port", "9999");List<Map<String, String>> servers = new ArrayList<>();servers.add(server);return new RestClient(restTemplate, servers);} }

?

類(lèi)

package com.thunisoft.maybee.engine.restclient;import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.ObjectMapper; import com.thunisoft.maybee.engine.request.RequestUtil; import com.thunisoft.maybee.engine.utils.JsonUtil; import com.thunisoft.maybee.engine.utils.MapUrlParamsUtils; import com.thunisoft.maybee.engine.utils.MapUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate;import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.*;/*** default RestTemplate class extends.*/ public class RestClient {private List<Map<String, String>> serverAddress;@Autowiredprivate RestTemplate restTemplate;public RestClient() {}public RestClient(RestTemplate restTemplate, List<Map<String, String>> serverAddress) {this.setRestTemplate(restTemplate);this.setServerAddress(serverAddress);}/*** 以GET方式操作服務(wù),主要用于查詢(xún)操作** @param serviceMethod* @param clazz* @param params* @param <T> 集合對(duì)象或者單一對(duì)象 統(tǒng)一使用 T.class* @return*/public <T> T getService(String serviceMethod, Class<T> clazz, Map<String, Object> params) {return excuteService("GET", serviceMethod, clazz, params, null);}/*** 以POST方式操作服務(wù),主要用于增刪改** @param serviceMethod* @param clazz* @param params* @param headers* @param <T> 增加、刪除、更改 建議使用 String.class* @return*/public <T> T postService(String serviceMethod, Class<T> clazz, Map<String, Object> params, HttpHeaders headers) {return excuteService("POST", serviceMethod, clazz, params, headers);}/*** 服務(wù)執(zhí)行方法** @param httpMethod* @param serviceMethod* @param clazz* @param params* @param headers* @param <T>* @return*/private <T> T excuteService(String httpMethod, String serviceMethod, Class<T> clazz, Map<String, Object> params, HttpHeaders headers) {Map<String, Object> otherParams = new HashMap<>(); // otherParams.put("accessToken", RequestUtil.getAccessToken());// put in the HTTP headers.if (headers == null) {headers = new HttpHeaders();}headers.add("accessToken", RequestUtil.getAccessToken());Map<String, Object> requestParams = MapUtils.addParams(params, otherParams);String urlParamsByMap = MapUrlParamsUtils.getUrlParamsByMap(requestParams);Map<String, String> randomOneServer = null;try {randomOneServer = this.getRandomOneServer();} catch (Exception e) {// nothing...System.out.println("server is null : " + e.getMessage());}URI uri = null;try {if (HttpMethod.POST.matches(httpMethod)) {uri = new URI(randomOneServer.get("scheme"), null, randomOneServer.get("host"), Integer.valueOf(randomOneServer.get("port")), serviceMethod, null, null);} else if (HttpMethod.GET.matches(httpMethod)) {uri = new URI(randomOneServer.get("scheme"), null, randomOneServer.get("host"), Integer.valueOf(randomOneServer.get("port")), serviceMethod, urlParamsByMap, null);}} catch (URISyntaxException e) {// nothing...System.out.println(e.getMessage());return null;}ObjectMapper mapper = new ObjectMapper();if (HttpMethod.GET.matches(httpMethod)) { // String forObject = this.getRestTemplate().getForObject(uri, String.class);HttpEntity httpEntity = new HttpEntity(headers);ResponseEntity<String> exchange = this.getRestTemplate().exchange(uri, HttpMethod.GET, httpEntity, String.class);String forObject = exchange.getBody();JsonUtil.JSON_TYPE jsonType = JsonUtil.getJSONType(forObject);try {switch (jsonType) {case JSON_TYPE_ARRAY:JavaType javaType = getCollectionType(mapper, ArrayList.class, clazz);return mapper.readValue(forObject, javaType);case JSON_TYPE_OBJECT:return mapper.readValue(forObject, clazz);default:return null;}} catch (IOException e) {// nothing...System.out.println(e.getMessage());return null;}} else if (HttpMethod.POST.matches(httpMethod)) {MultiValueMap reqParams = new LinkedMultiValueMap();Set<Map.Entry<String, Object>> entries = requestParams.entrySet();for (Map.Entry<String, Object> entry : entries) {reqParams.add(entry.getKey(), entry.getValue());}HttpEntity httpEntity = new HttpEntity(reqParams, headers);return this.getRestTemplate().postForObject(uri.toString(), httpEntity, clazz);} else {return null;}}/*** 獲取泛型的Collection Type** @param collectionClass 泛型的Collection* @param elementClasses 元素類(lèi)* @return JavaType Java類(lèi)型* @since 1.0*/private JavaType getCollectionType(ObjectMapper mapper, Class<?> collectionClass, Class<?>... elementClasses) {return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);}/*** 隨機(jī)獲取一個(gè)服務(wù)** @return*/private Map<String, String> getRandomOneServer() throws Exception {if (this.getServerAddress() == null) {throw new Exception("配置為空!");}Random rand = new Random();return this.getServerAddress().get(rand.nextInt(this.getServerAddress().size()));}public RestTemplate getRestTemplate() {return restTemplate;}public void setRestTemplate(RestTemplate restTemplate) {this.restTemplate = restTemplate;}public List<Map<String, String>> getServerAddress() {return serverAddress;}public void setServerAddress(List<Map<String, String>> serverAddress) {this.serverAddress = serverAddress;} }

?

轉(zhuǎn)載于:https://www.cnblogs.com/hfultrastrong/p/9075973.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的两个服务之间的调用请求的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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