请自行揣摩代码

package com.hudai.platform.manager.util;

import java.net.URI;
import java.net.URISyntaxException; import javax.annotation.Resource; import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSONObject;
import com.hudai.platform.manager.config.BaseEnum;
import com.hudai.platform.manager.model.BaseResponse; /**
* @author WanHongLei
* @version 创建时间:2019年2月19日 下午3:09:13 类说明
*/
@Component
public class RestApi {
private static final Logger logger = LoggerFactory.getLogger(RestApi.class); @Resource
private RestTemplate restTemplate; @Value("${xjd.applications.url}")
private String url; @Value("${clientCredentials}")
private String clientCredentials; public BaseResponse<JSONObject> proxy(Object obj, String path) {
URI uri;
try {
uri = new URI(url + path);
} catch (URISyntaxException e) {
logger.error("URI构建失败", e);
return new BaseResponse<>(BaseEnum.FAILED.getCode(), BaseEnum.FAILED.getName());
} String base64ClientCredentials = new String(Base64.encodeBase64(clientCredentials.getBytes())); RequestEntity<Object> requestEntity = RequestEntity.post(uri).header("Authorization", "Basic " + base64ClientCredentials).contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON).body(obj);
ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(requestEntity, JSONObject.class);
JSONObject jsonObj = responseEntity.getBody();
if(responseEntity.getStatusCode() == HttpStatus.OK){
return new BaseResponse<>(jsonObj);
}else{
logger.error("请求失败,errmsg = " + jsonObj.toJSONString());
return new BaseResponse<>(jsonObj.getIntValue("error"), jsonObj.getString("msg"));
}
}
}

application.properties中添加:clientCredentials=user:password

最新文章

  1. Ruby--strftime
  2. Go语言中的管道(Channel)总结
  3. poj1458 dp入门
  4. ORA-01157报错&quot;cannot identify/lock data file&quot;解决
  5. partial_sort_百度百科
  6. 为什么需要DevOps
  7. 将Web项目访问的URL项目名设置为&quot;/&quot;
  8. Hbase Scan的方法
  9. python+selenium自动测试之WebDriver的常用API(基础篇一)
  10. 抖音分享和授权(iOS)
  11. Python Pycharm 专题
  12. [总结]jQuery之常用函数方法参考手册
  13. Codeforces.666A.Reberland Linguistics(DP)
  14. Qt模态与非模态
  15. 初认识ZK
  16. Text Relatives
  17. Git学习系列之Git基本操作推送项目(图文详解)
  18. 使用$http.post()提交数据后台接收不到
  19. ubuntu下安装myeclipse+破解
  20. 一张图说明HTTPS 握手过程

热门文章

  1. wepy一些问题和解决方案
  2. 问题:Could not install packages due to an EnvironmentError: [Errno 13] Permission denied:
  3. [Wolfgang Mauerer] 深入linux 内核架构 第二章 进程管理与调度【未完】
  4. ngin负载均衡集群(一)
  5. beautifulsoup解析
  6. B1056 组合数的和 (15分)
  7. as API一些容易忘记的属性和方法
  8. 3224: Tyvj 1728 普通平衡树(finger tree)
  9. day23 Model 操作,Form 验证以及序列化操作
  10. Couchbase I