Spring boot 如何在SpringBoot RestTemplate中发送json请求正文和请求头
UsedSpring boot 如何在SpringBoot RestTemplate中发送json请求正文和请求头,spring-boot,Spring Boot,UsedRestTemplate以下代码仅发送正文详细信息。请求上不存在标题详细信息。请帮助我一起发送请求正文和请求标题 public TResponse getDynamicComputationValue( Map<String, String> reqHeaders, Map<String, Object> reqObj) { TResponse result = new TResponse(false, "JWT Request
RestTemplate
以下代码仅发送正文详细信息。请求上不存在标题详细信息。请帮助我一起发送请求正文和请求标题
public TResponse getDynamicComputationValue(
Map<String, String> reqHeaders, Map<String, Object> reqObj) {
TResponse result = new TResponse(false, "JWT Request failed");
String url = "http://localhost/exp/test/gd.php";
String userKey = "";
try {
// create headers
HttpHeaders headers = new HttpHeaders();
for (Map.Entry<String, String> pair : reqHeaders.entrySet()) {
String headerKey = pair.getKey().toLowerCase().trim();
if ("userkey".equals(headerKey)) {
headers.add("UserKey", pair.getValue());
userKey = pair.getValue();
}
if ("content-type".equals(headerKey)) {
headers.add("Content-Type", pair.getValue());
}
if ("authorization".equals(headerKey)) {
headers.add("Authorization", pair.getValue());
}
}
// HttpEntity<String> request = new HttpEntity<>("body", headers);
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> request = new HttpEntity<String>(reqObj.toString(), headers);
System.out.println(request.toString() + "<-- req -->");
ResponseEntity<String> response = this.restTemplate.exchange(
url, HttpMethod.POST, request, String.class
);
// result.message = response;
// System.out.println("response ----------------------->" + response);
} catch (RestClientException e) {
result.message = e.getMessage();
System.out.println("error response ----------------------->" + e.getMessage());
}
return result;
}
您正在发送带有正文和标题的请求中的
request=newhttpentity(“body”,headers)
HttpEntity
{condition_2=[{"object_variable_name":"repairing_maintainance_cost", "object_variable_value":"10000"}, {"object_variable_name":"chaargable_in_perc", "object_variable_value":"10"}], condition_1=[{"object_variable_name":"flat_type", "object_variable_value":"A"}]}