1. 程式人生 > >Spring MVC返回多重的Json數據

Spring MVC返回多重的Json數據

eat empty keywords set nes string arraylist shm spa

一、需求:

頁面返回數據

 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數據