post方法中若是使用map傳參,須要使用MultiValueMap來傳遞
String url => 顧名思義 這個參數是請求的url路徑app
Object request => 請求的body 這個參數須要再controller類用 @RequestBody 註解接收ide
Class
第四個參數 postForObject 方法多種重構測試
Map<String,?> uriVariables => uri 變量 顧名思義 這是放置變量的地方this
Object... uriVariables => 可變長 Object 類型 參數url
@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上拼接參數並使用{參數名}佔位符站位idea
而後將參數放到 第四個參數 可變長 Object 參數上 便可.net
Controller類代碼rest
@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傳值也很簡單code
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