LinkedMultiValueMap<>()里面的value 改为自定义的类之后,完整的restTemplate 发送get,formdata形式
时间: 2024-02-20 19:00:38 浏览: 68
好的,下面是一个使用 `LinkedMultiValueMap` 和自定义类的完整示例,它使用 `RestTemplate` 发送 GET 请求,提交表单数据。
```java
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
public class Main {
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();
// 创建请求头
HttpHeaders headers = new HttpHeaders();
headers.add("User-Agent", "Mozilla/5.0 Firefox/26.0");
headers.add("Accept-Language", "en-US,en;q=0.5");
// 创建请求参数
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("username", "john");
params.add("password", "secret");
// 创建请求体
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<>(params, headers);
// 发送请求
String url = "http://localhost:8080/login";
ResponseEntity<Person[]> responseEntity = restTemplate.exchange(
url,
HttpMethod.GET,
requestEntity,
Person[].class);
// 获取响应
Person[] persons = responseEntity.getBody();
// 输出响应
for (Person person : persons) {
System.out.println(person);
}
}
}
class Person {
private String name;
private int age;
public Person() {}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return name + " (age " + age + ")";
}
}
```
在上面的示例中,我们首先创建了一个 `RestTemplate` 对象,然后创建了请求头和请求参数。我们使用 `LinkedMultiValueMap` 来创建了一个 `params` 对象,这个对象的键是 `String` 类型,值是 `String` 类型的列表。您可以将其中的 `String` 类型改为您自定义的类。
接下来,我们使用 `HttpEntity` 创建了一个请求体,将请求头和请求参数传递给构造函数。我们使用 `exchange()` 方法发送了 GET 请求,提交了表单数据。注意,`exchange()` 方法的第四个参数是响应的类型,这里我们使用 `Person[].class` 来表示响应是一个 `Person` 类型的数组。
最后,我们输出了响应中的每个 `Person` 对象,以演示如何解析和使用响应。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)