resttemplate 请求post 发送json参数
时间: 2023-11-14 21:59:25 浏览: 49
使用 RestTemplate 发送 POST 请求并发送 JSON 参数的示例代码如下:
```
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
JSONObject requestBody = new JSONObject();
requestBody.put("key1", "value1");
requestBody.put("key2", "value2");
HttpEntity<String> request = new HttpEntity<String>(requestBody.toString(), headers);
String url = "http://example.com/api/endpoint";
ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
```
其中,`HttpHeaders` 设置请求头信息,`JSONObject` 构造请求体 JSON 对象,`HttpEntity` 将请求体和请求头组合成一个请求实体对象,`RestTemplate` 发送 POST 请求并返回响应结果。
相关问题
resttemplate post请求发送json数据
您可以使用RestTemplate的postForObject方法发送POST请求并发送JSON数据。下面是一个示例代码:
```java
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
public class RestClient {
public static void main(String[] args) {
// 创建RestTemplate实例
RestTemplate restTemplate = new RestTemplate();
// 设置请求头,指定Content-Type为application/json
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 设置请求体,即要发送的JSON数据
String jsonBody = "{\"key\": \"value\"}";
// 创建HttpEntity对象,包含请求头和请求体
HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);
// 发送POST请求并获取响应
String url = "http://example.com/api/endpoint"; // 请求的URL
String response = restTemplate.postForObject(url, requestEntity, String.class);
// 处理响应
System.out.println(response);
}
}
```
在上面的代码中,您需要将`http://example.com/api/endpoint`替换为实际的目标URL。另外,您可以根据需要调整JSON请求体的内容。
resttemplate发送post请求多个参数
可以通过构建一个 Map 来传递多个参数,然后将其设置为 HttpEntity 的 body 参数,最后通过 RestTemplate 发送 POST 请求。
示例代码如下:
```java
RestTemplate restTemplate = new RestTemplate();
String url = "http://example.com/api";
Map<String, Object> params = new HashMap<>();
params.put("param1", "value1");
params.put("param2", "value2");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, headers);
String result = restTemplate.postForObject(url, request, String.class);
```
在这个例子中,我们将参数放入一个 Map 中,然后设置请求头的 Content-Type 为 application/json,将 Map 设置为请求体,最后通过 RestTemplate 发送 POST 请求,得到响应结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)