Springboot使用RestTemplate发送Post请求postForEntity (application/json)的坑

  • Post author:
  • Post category:其他



当使用RestTemplate进行http请求时,的确很方便,但是当需要进行post请求时遇到了坑


1POST传递参数 :采用 LinkedMultiValueMap ,不能使用HashMap

 

  1. String url = 'http://posturl';


  2. MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();


  3. map.add("shopid","1");


  4. HttpHeaders headers = new HttpHeaders();


  5. headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);


  6. HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);


  7. return restTemplate.postForEntity(url, request,String.class);

  • 注意 上面的请求支持参数类型均为String类型,如果MultiValueMap<String, Object>  LinkedMultiValueMap 会将value放进list中,key –> [value]


2 采用JSONObject或者实体类传递参数

 

  1. RestTemplate restTemplate = new RestTemplate();


  2. HttpHeaders headers = new HttpHeaders();


  3. MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");


  4. headers.setContentType(type);


  5. headers.add("Accept", MediaType.APPLICATION_JSON.toString());


  6. JSONObject param = new JSONObject();


  7. param.put("username", "123");


  8. HttpEntity<String> formEntity = new HttpEntity<String>(param, headers);


  9. String result = restTemplate.postForObject(url, formEntity, String.class);

  • 使用这种方式可以传递任意类型的参数,避免上面的情况

总结,遇到这种问题时多看看源码会很有帮助。

 

  1. public class LinkedMultiValueMap<K, V> implements MultiValueMap<K, V>, Serializable, Cloneable {


  2. private static final long serialVersionUID = 3801124242820219131L;


  3. private final Map<K, List<V>> targetMap;


  4. public LinkedMultiValueMap() {


  5. this.targetMap = new LinkedHashMap();


  6. }


  7. public LinkedMultiValueMap(int initialCapacity) {


  8. this.targetMap = new LinkedHashMap(initialCapacity);


  9. }


  10. public LinkedMultiValueMap(Map<K, List<V>> otherMap) {


  11. this.targetMap = new LinkedHashMap(otherMap);


  12. }


  13. @Nullable


  14. public V getFirst(K key) {


  15. List<V> values = (List)this.targetMap.get(key);


  16. return values != null && !values.isEmpty() ? values.get(0) : null;


  17. }


  18. public void add(K key, @Nullable V value) {


  19. List<V> values = (List)this.targetMap.computeIfAbsent(key, (k) -> {


  20. return new LinkedList();


  21. });


  22. values.add(value);


  23. }


  24. public void addAll(K key, List<? extends V> values) {


  25. List<V> currentValues = (List)this.targetMap.computeIfAbsent(key, (k) -> {


  26. return new LinkedList();


  27. });


  28. currentValues.addAll(values);


  29. }


  30. public void addAll(MultiValueMap<K, V> values) {


  31. Iterator var2 = values.entrySet().iterator();


  32. while(var2.hasNext()) {


  33. Entry<K, List<V>> entry = (Entry)var2.next();


  34. this.addAll(entry.getKey(), (List)entry.getValue());


  35. }


  36. }


  37. public void set(K key, @Nullable V value) {


  38. List<V> values = new LinkedList();


  39. values.add(value);


  40. this.targetMap.put(key, values);


  41. }


  42. public void setAll(Map<K, V> values) {


  43. values.forEach(this::set);


  44. }


  45. public Map<K, V> toSingleValueMap() {


  46. LinkedHashMap<K, V> singleValueMap = new LinkedHashMap(this.targetMap.size());


  47. this.targetMap.forEach((key, values) -> {


  48. if (values != null && !values.isEmpty()) {


  49. singleValueMap.put(key, values.get(0));


  50. }


  51. });


  52. return singleValueMap;


  53. }

转自:

https://blog.csdn.net/justin_bob/article/details/103019115