使用RestTemplate发送post JSON请求

阅读更多
private final String BASE_URL = "http://10.0.0.4:9200/";
private final String URL = BASE_URL + "t1_mms_sku_supplier/sku_supplier/_search";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
HttpEntity entity = new HttpEntity<>(requestJsonParam, headers);
log.info("RequestEntity:{}", JsonUtils.ToJsonString(entity));
即参数是JSON格式的字符串,返回值为字符串
ResponseEntity responseEntity = restTemplate.postForEntity(URL, entity, String.class);
log.info("ResponseEntity:{}", responseEntity != null ? JsonUtils.ToJsonString(responseEntity) : null);

比如requestJsonParam是下面的值,
{
"query": {
"bool": {
"filter": [{
"terms": {
"shopCode": [
"W000"
],
"boost": 1.0
}
}
],
"disable_coord": false,
"adjust_pure_negative": true,
"boost": 1.0
}
}
}

你可能感兴趣的:(json,restTemplate,spring,http,post)