java-htttp-远程访问之RestTemplate,json
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                java-htttp-远程访问之RestTemplate,json
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                遠(yuǎn)程訪問之RestTemplate
相關(guān)資料
 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();/*** 遠(yuǎn)程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("請求響應(yīng)異常");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 {/** 將工號集合轉(zhuǎn)成http訪問入?yún)? */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字符串轉(zhuǎn)成http頭像訪問的出參* */public static Map<String, String> getIConResult(String result) {Map<String,String> map = JSON.parseObject(result, Map.class);return map;} }restTemplate調(diào)用遠(yuǎn)程服務(wù)
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();} }總結(jié)
以上是生活随笔為你收集整理的java-htttp-远程访问之RestTemplate,json的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 贝叶斯滤波和贝叶斯平滑(Kalman滤波
- 下一篇: ui svg 转纯svg_带有SVG和边
