class java.util.ArrayList 无法转换为 org.springframework.util.MultiValueMap

huangapple go评论93阅读模式
英文:

class java.util.ArrayList cannot be cast to class org.springframework.util.MultiValueMap

问题

  1. [
  2. {
  3. "name": "巴布亚新几内亚",
  4. "value": "PG"
  5. },
  6. {
  7. "name": "美国",
  8. "value": "US"
  9. },
  10. ...
  11. ]
  1. public Map<String, Object> getCountryNameCodeList() {
  2. String[] countryCodes = Locale.getISOCountries();
  3. Map<String, Object> list = new HashMap<>();
  4. for (String countryCode : countryCodes) {
  5. Locale obj = new Locale("", countryCode);
  6. list.put(obj.getDisplayCountry(), obj.getCountry());
  7. }
  8. return list;
  9. }
  10. @GetMapping("shipping_countries")
  11. public ResponseEntity<List<Map<String, String>>> getShippingCountries() {
  12. Map<String, Object> list = countriesService.getCountryNameCodeList();
  13. List<Map<String, String>> countries = new ArrayList<>();
  14. for (Map.Entry<String, Object> entry : list.entrySet()) {
  15. Map<String, String> country = new HashMap<>();
  16. country.put("name", entry.getKey());
  17. country.put("value", entry.getValue().toString());
  18. countries.add(country);
  19. }
  20. return new ResponseEntity<>(countries, HttpStatus.OK);
  21. }

出现错误的原因是在返回ResponseEntity时,您尝试将dtos(类型为ArrayList)转换为MultiValueMap,这是不正确的。我已经根据您的代码对其进行了修复,现在应该能够获得您所期望的 JSON 响应,并且不会再出现错误。

英文:

I want to generate a JSON response which includes inner JSON Objects. I tried this:

  1. public Map&lt;String, Object&gt; getCountryNameCodeList() {
  2. String[] countryCodes = Locale.getISOCountries();
  3. Map&lt;String, Object&gt; list = new HashMap&lt;&gt;();
  4. for (String countryCode : countryCodes) {
  5. Locale obj = new Locale(&quot;&quot;, countryCode);
  6. list.put(obj.getDisplayCountry().toString(), obj.getCountry());
  7. }
  8. return list;
  9. }

Rest Api:

  1. @GetMapping(&quot;shipping_countries&quot;)
  2. public ResponseEntity&lt;Set&lt;Map.Entry&lt;String, Object&gt;&gt;&gt; getShippingCountries() {
  3. Map&lt;String, Object&gt; list = countriesService.getCountryNameCodeList();
  4. List&lt;KeyValueDTO&gt; dtos = new ArrayList();
  5. for(Map.Entry&lt;String, Object&gt; value: list.entrySet()) {
  6. KeyValueDTO dto = new KeyValueDTO();
  7. dto.setKey(value.getKey());
  8. dto.setValue(value.getValue().toString());
  9. dtos.add(dto);
  10. }
  11. return new ResponseEntity&lt;Set&lt;Map.Entry&lt;String, Object&gt;&gt;&gt;((MultiValueMap&lt;String, String&gt;) dtos, HttpStatus.OK);
  12. }

I want to get this response:

  1. [
  2. {
  3. name: &quot;Papua New Guinea&quot;,
  4. value: &quot;PG&quot;
  5. },
  6. {
  7. name: &quot;Unites States&quot;,
  8. value: &quot;US&quot;
  9. },
  10. ....
  11. ]

But I get this error:

  1. class java.util.ArrayList cannot be cast to class org.springframework.util.MultiValueMap (java.util.ArrayList is in module java.base of loader &#39;bootstrap&#39;; org.springframework.util.MultiValueMap is in unnamed module of loader org.springframework.boot.loader.LaunchedURLClassLoader @6267c3bb)

How I can fix this issue?

答案1

得分: 1

似乎您正在将一个列表 List<KeyValueDTO> 强制转换为一个多映射 (MultiValueMap<String, String>)。它们是完全不同的类型 - 列表不是映射。因此,在您的帖子中出现了异常。尝试在不进行转换的情况下返回该值。

英文:

It seems you are typecasting List&lt;KeyValueDTO&gt; a list into (MultiValueMap&lt;String, String&gt;) a multimap. They are totally different types - a list is not a map. Hence, the exception in your post. Try to return the value without the cast.

huangapple
  • 本文由 发表于 2020年10月3日 22:58:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/64185581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定