RestTemplate在Hearder中传参

RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("siteId", siteId);
HttpEntity requestEntity = new HttpEntity<>(null, headers);
JSONObject body = client.exchange(url, HttpMethod.GET, requestEntity, JSONObject.class).getBody();

 

 

 

 

第2种方式:

package org.springframework.http;

 

 

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
headers.add("X-Requested-With", "XMLHttpRequest");

MultiValueMap params = new LinkedMultiValueMap<>();

params.add("productName", "缴纳");
params.add("orderPeriod", "60");

HttpEntity> requestEntity = new HttpEntity<>(params, headers);
String body = restTemplate.postForObject(url, requestEntity, String.class);
System.out.println(body);

JSONObject data = JSON.parseObject(body);

你可能感兴趣的:(java)