欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用RestTemplate发送post JSON请求

程序员文章站 2022-03-12 11:27:54
...
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<String> entity = new HttpEntity<>(requestJsonParam, headers);
log.info("RequestEntity:{}", JsonUtils.ToJsonString(entity));
即参数是JSON格式的字符串,返回值为字符串
ResponseEntity<String> 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
}
}
}