返回Spring Rest API中的哈希映射。

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

Return Hashmap from Spring Rest API

问题

我想使用这段代码从Rest API获取一些数据:

  1. public Map<Integer, String> getCategoriesList() {
  2. Map<Integer, String> list = new HashMap<>();
  3. list.put(1, "Electronics");
  4. list.put(2, "Outdoor and Sports");
  5. list.put(3, "Home and Garden");
  6. list.put(4, "Home appliances");
  7. list.put(5, "Air conditioners and heaters");
  8. list.put(6, "IT accessories");
  9. list.put(7, "Photo and Video");
  10. list.put(8, "TV Video and Gaming");
  11. return list;
  12. }
  13. @GetMapping("categories")
  14. public ResponseEntity<List<String>> getCategoriesList() {
  15. return (ResponseEntity<List<String>>) categoriesService.getCategoriesList();
  16. }

我得到错误:class java.util.HashMap cannot be cast to class org.springframework.http.ResponseEntity

以何种适当的方式将这些数据作为响应返回?

英文:

I want to use this code to get some data from Rest API:

  1. public Map&lt;Integer, String&gt; getCategoriesList() {
  2. Map&lt;Integer, String&gt; list = new HashMap&lt;&gt;();
  3. list.put(1, &quot;Electronics&quot;);
  4. list.put(2, &quot;Outdoor and Sports&quot;);
  5. list.put(3, &quot;Home and Garden&quot;);
  6. list.put(4, &quot;Home appliances&quot;);
  7. list.put(5, &quot;Air conditioners and heaters&quot;);
  8. list.put(6, &quot;IT accessories&quot;);
  9. list.put(7, &quot;Photo and Video&quot;);
  10. list.put(8, &quot;TV Video and Gaming&quot;);
  11. return list;
  12. }
  13. @GetMapping(&quot;categories&quot;)
  14. public ResponseEntity&lt;List&lt;String&gt;&gt; getCategoriesList() {
  15. return (ResponseEntity&lt;List&lt;String&gt;&gt;) categoriesService.getCategoriesList();
  16. }

I get error: class java.util.HashMap cannot be cast to class org.springframework.http.ResponseEntity

What is the appropriate way to return this data as a response?

答案1

得分: 2

你不能像那样将一个类型强制转换为另一个类型... 尝试这样做:

使用ResponseEntity包装:

  1. @GetMapping("categories")
  2. public ResponseEntity<Map<Integer, String>> getCategoriesList() {
  3. return new ResponseEntity<Map<Integer, String>>(categoriesService.getCategoriesList(), HttpStatus.OK);
  4. }

不使用ResponseEntity包装:

  1. @GetMapping("categories")
  2. @ResponseStatus(code = HttpStatus.OK)
  3. public Map<Integer, String> getCategoriesList() {
  4. return categoriesService.getCategoriesList();
  5. }

因为这两种类型的映射都为Jackson所知(我假设您在Spring中用于序列化/反序列化的是Jackson),所以不需要做任何其他操作。

参考链接:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

英文:

You cannot cast one type to another like that...try this

ResponseEntity type:

  1. @GetMapping(&quot;categories&quot;)
  2. public ResponseEntity&lt;Map&lt;Integer, String&gt;&gt; getCategoriesList() {
  3. return new ResponseEntity&lt;Map&lt;Integer,String&gt;&gt;(categoriesService.getCategoriesList(), HttpStatus.OK);
  4. }

Without ResponseEntity wrapper

  1. @GetMapping(&quot;categories&quot;)
  2. @ResponseStatus(code = HttpStatus.OK)
  3. public Map&lt;Integer, String&gt; getCategoriesList() {
  4. return categoriesService.getCategoriesList();
  5. }

Since both types of map are known to jackson (I presume that's what you are using in spring for serialization/deserialization), no need to do anything more.

Reference:
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/http/ResponseEntity.html

huangapple
  • 本文由 发表于 2020年9月29日 06:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64110371.html
匿名

发表评论

匿名网友

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

确定