• 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 }
  • 相关阅读:
    Vue-CLI
    Vue生命周期函数
    构建之法阅读笔记之四
    大二下个人总结
    个人加分项
    对老师的建议
    学习进度条 第九十一-第一百零五天 vue+uniapp app开发学习笔记
    第15周作业
    二进制安装mysql 5.7.31 启动报错/etc/init.d/mysqld: line 239: my_print_defaults: command not found
    获取最小数字
  • 原文地址:https://www.cnblogs.com/Drajun/p/9344692.html
Copyright © 2020-2023  润新知