标签:csdn ons 百度 字符 string type multi ret val
post方法中如果使用map传参,需要使用MultiValueMap来传递
String url => 顾名思义 这个参数是请求的url路径
Object request => 请求的body 这个参数需要再controller类用 @RequestBody 注解接收
Class
第四个参数?postForObject 方法多种重构
Map<String,?> uriVariables => uri 变量 顾名思义 这是放置变量的地方
Object...?uriVariables =>?可变长 Object 类型 参数
@Nullable
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Object... uriVariables) throws RestClientException {
RequestCallback requestCallback = this.httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters(), this.logger);
return this.execute(url, HttpMethod.POST, requestCallback, responseExtractor, (Object[])uriVariables);
}
@Nullable
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
RequestCallback requestCallback = this.httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters(), this.logger);
return this.execute(url, HttpMethod.POST, requestCallback, responseExtractor, (Map)uriVariables);
}
@Nullable
public <T> T postForObject(URI url, @Nullable Object request, Class<T> responseType) throws RestClientException {
RequestCallback requestCallback = this.httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters());
return this.execute(url, HttpMethod.POST, requestCallback, responseExtractor);
}
@Service
public class HelloService {
@Autowired
RestTemplate restTemplate;
public String helloService(String name,Integer age){
return restTemplate.postForObject("http://SERVICE-HELLO/hello?name={name}&age={age}", null, String.class, name,age);
}
}
需要再url上拼接参数并使用{参数名}占位符站位
然后将参数放到 第四个参数 可变长 Object 参数上 即可
Controller类代码
@RestController
public class DemoController {
@Value("${server.port}")
String port;
@PostMapping("hello")
public String home(String name,Integer age){
return "hello " + name + " you age is " + age + " ,i am from port:" + port;
}
}
map传值也很简单
public String helloService(String name,Integer age){
Map<String,Object> map = new HashMap<>();
map.put("name",name);
map.put("age",age);
return restTemplate.postForObject("http://SERVICE-HELLO/hello?name={name}&age={age}", null, String.class, map);
}
只需要将参数放入到map中即可
那有些人要问了 , 为什么不能用 第二个 request 参数传值 , 其实是可以的
我试过用HashMap 和 LinkedHashMap 都是接收不到的
所以我们来看一下源码是怎么写的
首先进入到 postForObject 方法中 发现request 参数 传入了一个 httpEntityCallBack 方法中 , 那么接着追踪
@Nullable
public <T> T postForObject(String url, @Nullable Object request, Class<T> responseType, Map<String, ?> uriVariables) throws RestClientException {
RequestCallback requestCallback = this.httpEntityCallback(request, responseType);
HttpMessageConverterExtractor<T> responseExtractor = new HttpMessageConverterExtractor(responseType, this.getMessageConverters(), this.logger);
return this.execute(url, HttpMethod.POST, requestCallback, responseExtractor, (Map)uriVariables);
}
进入httpEntityCallBack方法中
httpEntityCallBack方法又调用了 RestTemplate的HttpEntityRequestCallback方法
public <T> RequestCallback httpEntityCallback(@Nullable Object requestBody, Type responseType) {
return new RestTemplate.HttpEntityRequestCallback(requestBody, responseType);
}
进入HttpEntityRequestCallback
这里会出现一个分支 instanceof 类型判定 requestBody 参数是否是 HttpEntity类型
public HttpEntityRequestCallback(@Nullable Object requestBody, @Nullable Type responseType) {
super(responseType);
if (requestBody instanceof HttpEntity) {
this.requestEntity = (HttpEntity)requestBody;
} else if (requestBody != null) {
this.requestEntity = new HttpEntity(requestBody);
} else {
this.requestEntity = HttpEntity.EMPTY;
}
}
如果不是则 创建一个HttpEntity类将 requestBody 参数传入
那么我们来看一下 HttpEntity 是怎么个构造
public HttpEntity(T body) {
this(body, (MultiValueMap)null);
}
public HttpEntity(MultiValueMap<String, String> headers) {
this((Object)null, headers);
}
这里可以看到 HttpEntity 有两个构造方法 一个是 传入 泛型的body 另一个是传入 MultiValueMap<String,String> headers
那么 这个MultiValueMap 是个什么东东
百度一下 发现
MultiValueMap 可以让一个key对应多个value,感觉是value产生了链表结构,可以很好的解决一些不好处理的字符串问题
那么我们来用这个奇怪的map实验一下
首先进入?MultiValueMap 接口 找到他的实现类
实现类到idea中查找
MultiValueMap 的实现类应该是?LinkedMultiValueMap
那么我们走起
public String helloService(String name,Integer age){
MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
paramMap.add("name",name);
paramMap.add("age", age);
return restTemplate.postForObject("http://SERVICE-HELLO/hello",paramMap,String.class);
}
controller代码
public class DemoController {
@Value("${server.port}")
String port;
@PostMapping("hello")
public String home(String name,Integer age){
return "MultiValueMap : hello " + name + " you age is " + age + " ,i am from port:" + port;
}
}
原文:https://blog.csdn.net/weixin_40461281/article/details/83472648
RestTemplate post请求使用map传参 Controller 接收不到值的解决方案 postForObject方法源码解析.md
标签:csdn ons 百度 字符 string type multi ret val
原文地址:https://www.cnblogs.com/eternityz/p/12241392.html