引入pom依赖
? ?<!--? RestTemplate ?-->
<dependency> ? ? ? ?<groupId>org.apache.httpcomponents</groupId> ? ? ? ?<artifactId>httpclient</artifactId> ? ? ? ?<version>4.5.10</version> ?</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> ? ? <groupId>com.alibaba</groupId> ? ? <artifactId>fastjson</artifactId> ? ? <version>1.2.60</version> </dependency>
postForObject方法
@Resource
private RestTemplate restTemplate;
//方法中代码
String url = ltUrlProperties.getCalculateUrl();
log.info("返回测算信息url======={}",url);
Map<String, Object> errReturn = new HashMap<>();
errReturn.put("code","500");
errReturn.put("message","测算失败");
errReturn.put("versionNo",versionCodeAndUcode.getVersionCode());
errReturn.put("uniqueId",versionCodeAndUcode.getUniqueId());
JSONObject jsonObject = JSONUtil.parseObj(errReturn);
String jsonStringErr = JSONUtil.toJsonStr(jsonObject);
// String jsonStringErr = JSON.toJSONString(ImmutableMap.of("userId", userId), SerializerFeature.WriteMapNullValue);
log.info("发送给龙头的测算返回信息=========={}",jsonStringErr);
HttpHeaders headersErr = new HttpHeaders();
headersErr.setContentType(MediaType.APPLICATION_JSON_UTF8);
headersErr.add("token", "我是token");
HttpEntity<String> httpEntityErr = new HttpEntity<>(jsonStringErr, headersErr);
String responseErr = this.restTemplate.postForObject(url, httpEntityErr, String.class);
log.info("龙头接收返回信息=========={}",responseErr);
getForObject方法
@Resource
private RestTemplate restTemplate;
String url = appConfig.getC1tm().getPushToC2()+"f_code="+c1InterfaceApi.getProjectCode()+"&version="+c1InterfaceApi.getVersion();
JSONObject result = this.restTemplate.getForObject(url,JSONObject.class);
log.info("返回数据:{}",result.toString());
exchange方法之post
Map<String, Object> requestMap = ImmutableMap.of("Parameters", parameters);
String jsonString = JSONObject.toJSONString(requestMap);
String requestURL = url;
log.info("=======调用接口: {}", requestURL);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "Basic " + encodeBase64String(username, password));
HttpEntity<String> httpEntity = new HttpEntity<>(jsonString, headers);
ResponseEntity<String> responseEntity = this.restTemplate.exchange(requestURL, HttpMethod.POST, httpEntity, String.class);
log.info("=======调用接口response: {}", responseEntity);
exchange方法之get
StringBuilder strBu = new StringBuilder();
strBu.append("?versionCode=").append(versionCode);
strBu.append("&projectCode=").append(projectCode);
String parseC3ExcelUrl = url;
parseC3ExcelUrl = parseC3ExcelUrl + strBu.toString();
String apiKey = this.config.getZncsConfig().getGaiaApiKey();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("X-Gaia-Api-Key",apiKey);
HttpEntity<String> entity = new HttpEntity<>("parameters",httpHeaders);
log.info("接口调用:"+parseC3ExcelUrl+"===="+apiKey);
ResponseEntity<String> responseEntity = restTemplate.exchange(parseC3ExcelUrl, HttpMethod.GET,entity,String.class);
log.info("接口响应:"+responseEntity.getStatusCode() + responseEntity.getBody());
x-www-form-urlencoded格式请求
String url = this.appConfig.getNcConfig().getTokenUrl();
String clientId = this.appConfig.getNcConfig().getClientId();
String clientSecret = this.appConfig.getNcConfig().getClientSecret();
String token = null;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> param = new LinkedMultiValueMap<>(3);
param.add("client_id", clientId);
param.add("client_secret", clientSecret);
param.add("grant_type", "client_credentials");
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(param, headers);
String result = restTemplate.postForObject(url, httpEntity, String.class);
if (StringUtils.isNotBlank(result)) {
JSONObject jsonResult = JSONObject.parseObject(result);
|