使用RestTemplate发送post JSON请求
程序员文章站
2022-03-12 10:44:37
...
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
}
}
}
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
}
}
}
下一篇: 虚拟DOM怎么实现?(代码示例)