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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java-htttp-远程访问之RestTemplate,json

發布時間:2024/3/26 编程问答 45 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java-htttp-远程访问之RestTemplate,json 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

遠程訪問之RestTemplate

相關資料
https://www.cnblogs.com/javazhiyin/p/9851775.html

Json處理
https://blog.csdn.net/herojuice/article/details/86132183

最佳工具類

@Slf4j public class HttpUtil {private static RestTemplate restTemplate = new RestTemplate();/*** 遠程http請求*/public static String remoteHttp(String url, HttpMethod httpMethod, Map<String, String> inHeader, String inBody) {HttpHeaders httpHeaders = new HttpHeaders();//json請求格式httpHeaders.setContentType(MediaType.APPLICATION_JSON);//封裝頭部信息if (CollectionUtils.isEmpty(inHeader)) {inHeader.forEach((x, y) -> {httpHeaders.add(x, y);});}//將頭部信息和body進行封裝HttpEntity<String> entity = new HttpEntity<>(inBody, httpHeaders);ResponseEntity<String> exchange = restTemplate.exchange(url, httpMethod, entity, String.class);if (exchange.getStatusCode() != HttpStatus.OK) {log.error("請求響應異常");return null;}return exchange.getBody();}/*** post請求*/public static String remotePost(String url, Map<String, String> inHeader, String inBody) {return remoteHttp(url,HttpMethod.POST,inHeader,inBody);} }

其他

public class JsonHandler {/** 將工號集合轉成http訪問入參* */public static String getIConParam(List<String> list,String channel,String appkey) {if(!CollectionUtils.isEmpty(list)){IConParam param = new IConParam();param.setChannel(channel);StringBuilder build = new StringBuilder(list.get(0));for(int i=1;i<list.size();i++){build.append(","+list.get(i));}String accounts = build.toString();param.setAccounts(accounts);String signBefore=appkey+"accounts"+accounts+"channel"+channel+appkey;System.out.println(signBefore);String sign = DigestUtils.md5DigestAsHex(signBefore.getBytes());param.setSign(sign);return JSON.toJSONString(param);}return null;}/** json字符串轉成http頭像訪問的出參* */public static Map<String, String> getIConResult(String result) {Map<String,String> map = JSON.parseObject(result, Map.class);return map;} }

restTemplate調用遠程服務

public class HttpRest {private static RestTemplate restTemplate;private static HttpHeaders httpHeaders;static {restTemplate = new RestTemplate();httpHeaders = new HttpHeaders();}/** post請求,返回string* */public static String getIConByList(String url, String body) {if((!StringUtils.isEmpty(body))&& (!StringUtils.isEmpty(url))){httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);HttpEntity<String> entity = new HttpEntity<>(body, httpHeaders);String result = restTemplate.exchange(url, HttpMethod.POST, entity, String.class).getBody();restTemplate.postForObject(urlDown,entity,null);return result;}return null;}} public class AnalyHttpUtil {private static RestTemplate restTemplate;static {restTemplate = new RestTemplate();}public static String analyPost(String url, HttpMethod httpMethod, Map<String, String> inHeader, String inBody) {HttpHeaders httpHeaders = new HttpHeaders();httpHeaders.setContentType(MediaType.APPLICATION_JSON);if (null != inHeader) {inHeader.forEach((x, y) -> {httpHeaders.add(x, y);});}HttpEntity<String> entity = new HttpEntity<>(inBody, httpHeaders);ResponseEntity<String> exchange = restTemplate.exchange(url, httpMethod, entity, String.class);if (exchange.getStatusCode()!= HttpStatus.OK){return null;}return exchange.getBody();} }

總結

以上是生活随笔為你收集整理的java-htttp-远程访问之RestTemplate,json的全部內容,希望文章能夠幫你解決所遇到的問題。

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