Springboot使用RestTemplate发送Post请求postForEntity (application-json)的坑| Id | Title | DateAdded | SourceUrl | PostType | Body | BlogId | Description | DateUpdated | IsMarkdown | EntryName | CreatedTime | IsActive | AutoDesc | AccessPermission |
| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------| -------------|
| 16334421| Springboot使用RestTemplate发送Post请求postForEntity (application/json)的坑| 2022-06-01T15:32:00| | BlogPost|
当使用RestTemplate进行http请求时,的确很方便,但是当需要进行post请求时遇到了坑
1POST传递参数 :采用 LinkedMultiValueMap ,不能使用HashMap
String url = 'http://posturl'; MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>(); map.add("shopid","1"); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers); return restTemplate.postForEntity(url, request,String.class);
注意 上面的请求支持参数类型均为String类型,如果MultiValueMap<String, Object> LinkedMultiValueMap 会将value放进list中,key --> [value]
2 采用JSONObject或者实体类传递参数
RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8"); headers.setContentType(type); headers.add("Accept", MediaType.APPLICATION_JSON.toString());JSONObject param </span>= <span style="color: #0000ff;">new</span><span style="color: #000000;"> JSONObject(); param.put(</span>"username", "123"<span style="color: #000000;">); HttpEntity</span><String> formEntity = <span style="color: #0000ff;">new</span> HttpEntity<String><span style="color: #000000;">(param, headers); String result </span>= restTemplate.postForObject(url, formEntity, String.<span style="color: #0000ff;">class</span>);</pre>
public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializable, Cloneable { private static final long serialVersionUID = 3801124242820219131L; private final Map<K, List<V>> targetMap;</span><span style="color: #0000ff;">public</span><span style="color: #000000;"> LinkedMultiValueMap() { </span><span style="color: #0000ff;">this</span>.targetMap = <span style="color: #0000ff;">new</span><span style="color: #000000;"> LinkedHashMap(); } </span><span style="color: #0000ff;">public</span> LinkedMultiValueMap(<span style="color: #0000ff;">int</span><span style="color: #000000;"> initialCapacity) { </span><span style="color: #0000ff;">this</span>.targetMap = <span style="color: #0000ff;">new</span><span style="color: #000000;"> LinkedHashMap(initialCapacity); } </span><span style="color: #0000ff;">public</span> LinkedMultiValueMap(Map<K, List<V>><span style="color: #000000;"> otherMap) { </span><span style="color: #0000ff;">this</span>.targetMap = <span style="color: #0000ff;">new</span><span style="color: #000000;"> LinkedHashMap(otherMap); } @Nullable </span><span style="color: #0000ff;">public</span><span style="color: #000000;"> V getFirst(K key) { List</span><V> values = (List)<span style="color: #0000ff;">this</span><span style="color: #000000;">.targetMap.get(key); </span><span style="color: #0000ff;">return</span> values != <span style="color: #0000ff;">null</span> && !values.isEmpty() ? values.get(0) : <span style="color: #0000ff;">null</span><span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> add(K key, @Nullable V value) { List</span><V> values = (List)<span style="color: #0000ff;">this</span>.targetMap.computeIfAbsent(key, (k) -><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> LinkedList(); }); values.add(value); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> addAll(K key, List<? <span style="color: #0000ff;">extends</span> V><span style="color: #000000;"> values) { List</span><V> currentValues = (List)<span style="color: #0000ff;">this</span>.targetMap.computeIfAbsent(key, (k) -><span style="color: #000000;"> { </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span><span style="color: #000000;"> LinkedList(); }); currentValues.addAll(values); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> addAll(MultiValueMap<K, V><span style="color: #000000;"> values) { Iterator var2 </span>=<span style="color: #000000;"> values.entrySet().iterator(); </span><span style="color: #0000ff;">while</span><span style="color: #000000;">(var2.hasNext()) { Entry</span><K, List<V>> entry =<span style="color: #000000;"> (Entry)var2.next(); </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.addAll(entry.getKey(), (List)entry.getValue()); } } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span><span style="color: #000000;"> set(K key, @Nullable V value) { List</span><V> values = <span style="color: #0000ff;">new</span><span style="color: #000000;"> LinkedList(); values.add(value); </span><span style="color: #0000ff;">this</span><span style="color: #000000;">.targetMap.put(key, values); } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> setAll(Map<K, V><span style="color: #000000;"> values) { values.forEach(</span><span style="color: #0000ff;">this</span><span style="color: #000000;">::set); } </span><span style="color: #0000ff;">public</span> Map<K, V><span style="color: #000000;"> toSingleValueMap() { LinkedHashMap</span><K, V> singleValueMap = <span style="color: #0000ff;">new</span> LinkedHashMap(<span style="color: #0000ff;">this</span><span style="color: #000000;">.targetMap.size()); </span><span style="color: #0000ff;">this</span>.targetMap.forEach((key, values) -><span style="color: #000000;"> { </span><span style="color: #0000ff;">if</span> (values != <span style="color: #0000ff;">null</span> && !<span style="color: #000000;">values.isEmpty()) { singleValueMap.put(key, values.get(</span>0<span style="color: #000000;">)); } }); </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> singleValueMap; }</span></pre>
| 648658| Springboot使用RestTemplate发送Post请求postForEntity (application/json)的坑| 2022-06-01T15:32:00| false| | 2022-06-01T15:32:18.11| true| 当使用RestTemplate进行http请求时,的确很方便,但是当需要进行post请求时遇到了坑 1POST传递参数 :采用 LinkedMultiValueMap ,不能使用HashMap String url = 'http://posturl'; MultiValueMap<String, | Anonymous| 标签:gt,postForEntity,RestTemplate,application,lt,values,new,-------------,public From: https://www.cnblogs.com/ralphlauren/p/18621301