Spring MVC返回多重的Json数据

Posted Drajun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring MVC返回多重的Json数据相关的知识,希望对你有一定的参考价值。

一、需求:

页面返回数据

 1 {
 2   "code": 0,
 3   "msg": "",
 4   "count": "2",
 5   "data": [{
 6       "keywords": "广东木材提供有限公司1",
 7       "frequency": 4621,
 8       "userNums": 4235
 9     },
10     {
11       "keywords": "广东木材提供有限公司2",
12       "frequency": 4621,
13       "userNums": 4235
14     }]
15 }

 

二、为此写了一个Firm类

 1 public class Firm {
 2 
 3     private String keywords;
 4     private String frequency;
 5     private String userNums;
 6     
 7     public Testdata(String keywords, String frequency, String userNums) {
 8         this.keywords = keywords;
 9         this.frequency = frequency;
10         this.userNums = userNums;
11     }
12 }

 

 

三、在Controller方法中

 1 @RequestMapping("/firm/listdata.action")
 2     public @ResponseBody Map<String, Object> listData(){
 3         
 4         List<Testdata> tdList = new ArrayList<>();
 5         Testdata td1 = new Testdata("广东木材提供有限公司1","4621","4235");
 6         Testdata td2 = new Testdata("广东木材提供有限公司1","4621","4235");
 7         tdList.add(td1);
 8         tdList.add(td2);
 9         tdList.add(td3);
10         tdList.add(td4);
11         tdList.add(td5);
12         Map<String, Object> map = new HashMap<>();
13         map.put("code", 0);
14         map.put("msg", "");
15         map.put("count", 2);
16         map.put("data", tdList);
17         return map;
18     }

 

四、访问时浏览器报500异常

org.springframework.http.converter.HttpMessageNotWritableException: 
Could not write content: No serializer found for class cn.ssm.trading.domain.Firm and 
no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0]);
nested exception is com.fasterxml.jackson.databind.JsonMappingException:
No serializer found for class cn.ssm.trading.domain.Firm and no properties discovered to create BeanSerializer
(to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
(through reference chain: java.util.HashMap["data"]->java.util.ArrayList[0])

 

五、解决

为Firm类实现序列化,为字段提供getter/setter以及无参构造方法

 1 public class Firm implements Serializable {
 2 
 3     private String keywords;
 4     private String frequency;
 5     private String userNums;
 6     
 7     public String getKeywords() {
 8         return keywords;
 9     }
10     public void setKeywords(String keywords) {
11         this.keywords = keywords;
12     }
13     public String getFrequency() {
14         return frequency;
15     }
16     public void setFrequency(String frequency) {
17         this.frequency = frequency;
18     }
19     public String getUserNums() {
20         return userNums;
21     }
22     public void setUserNums(String userNums) {
23         this.userNums = userNums;
24     }
25     //无参构造方法
26     public Testdata() {
27         
28     }
29     
30     public Testdata(String keywords, String frequency, String userNums) {
31         this.keywords = keywords;
32         this.frequency = frequency;
33         this.userNums = userNums;
34     }
35 }

 







以上是关于Spring MVC返回多重的Json数据的主要内容,如果未能解决你的问题,请参考以下文章

spring mvc 返回JSON数据

spring mvc 返回json数据的三种方式

Spring mvc 实现jsonp和json数据类型

Spring MVC全局异常后返回JSON异常数据

Spring MVC 返回Json数据环境记录

Spring MVC返回Map格式JSON数据