你可以使用 RestTemplate 发送一个带有 JSON 数据的 POST 请求,代码示例如下:
RestTemplate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.client.RestTemplate;
...
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON);
String jsonData = "{\"field1\":\"value1\",\"field2\":\"value2\"}"; HttpEntity<String> entity = new HttpEntity<>(jsonData, headers);
String url = "http://example.com/api/v1/resource"; String result = restTemplate.postForObject(url, entity, String.class);
|
以上示例来自chatGPT