使用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
}
}
}
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
}
}
}
推荐阅读
-
php模拟post请求发送文件_PHP教程
-
php模拟post请求发送文件
-
PHP中使用cURL实现Get和Post请求的方法
-
Python批量发送post请求的实现代码
-
php 利用socket发送HTTP请求(GET,POST),socketget_PHP教程
-
PHP模拟发送POST请求之三、加强file_get_contents发送POST请求
-
Python 使用requests模块发送GET和POST请求的实现代码
-
java网络编程中向指定URL发送GET POST请求示例
-
Android中使用HttpURLConnection实现GET POST JSON数据与下载图片
-
Android使用httpPost向服务器发送请求的方法