resttemplate get
时间: 2023-08-24 12:12:42 浏览: 46
RestTemplate可以通过以下方式发送带参数的GET请求:
1. 使用URI模板变量
```java
RestTemplate restTemplate = new RestTemplate();
String url = "http://example.com/api/{id}";
Map<String, String> params = new HashMap<>();
params.put("id", "123");
String result = restTemplate.getForObject(url, String.class, params);
```
2. 使用URI参数
```java
RestTemplate restTemplate = new RestTemplate();
String url = "http://example.com/api?id={id}";
String result = restTemplate.getForObject(url, String.class, "123");
```
3. 使用URI构建器
```java
RestTemplate restTemplate = new RestTemplate();
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://example.com/api")
.queryParam("id", "123");
String url = builder.toUriString();
String result = restTemplate.getForObject(url, String.class);
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)