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 }

原文地址:https://www.cnblogs.com/Drajun/p/9344692.html

时间: 2024-07-31 17:29:12

Spring MVC返回多重的Json数据的相关文章

Spring MVC返回Map格式JSON数据

问题描述: ajax中走error : function(e) {} 问题背景: 在测试controller层时,试过了ResponseEntity<ResponseModel>这种类型返回,这是可行的,但是出于好奇,想看看Map返回可不可行.结果出乎我预料,返回Map时JSP页面总是走error : function(e) {},这就奇怪了,刚才用ResponseEntity返回可行,而Map为什么不行呢?于是就查了ajax走error:function的原因, 原因: 1. 后台出错 2.

Spring mvc 用ajax传输json数据

在spring mvc3中,已经集成了Jackson(json处理器)来处理数据输出json格式,spring中封装的类是 org.springframework.http.converter.json.MappingJackson2HttpMessageConverter这个json转换器, 如果是springmvc3.2之前的版本,可以使用org.springframework.http.converter.MappingJacksonHttpMessageConverter这个json转换

spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable

1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(value = { "/actor_details" }, method = { RequestMethod.POST }) @ResponseBody public ResultObject actorDetails(@RequestBody ActorDetailsRequest req)

spring mvc返回json字符串的方式

spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring-servlet.xml文件中配置如下代码 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans&quo

spring -mvc 将对象封装json返回时删除掉对象中的属性注解方式

spring -mvc 将对象封装json返回时删除掉对象中的属性注解方式 在类名,接口头上注解使用在 @JsonIgnoreProperties(value={"comid"}) //希望动态过滤掉的属性 例 @JsonIgnoreProperties(value={"comid"}) public interface 接口名称{ } @JsonIgnoreProperties(value={"comid"}) public class 类名{

关于 Spring MVC 返回 json 字符串

Spring MVC 返回 json 字符串的几种方式: 1.通过 @ResponseBody 注解自动将返回对象转为 json,这种方法需要 jackson-core.jar 等相关 jar 包支持,项目如果通过 maven 管理,直接在 maven 中加入依赖: ,这样在 maven 依赖中会新增完整的三个 jar 包: 2.在 spring-MVC 配置文件中进行默认视图配置: 3.借助其他 json 工具进行转换:比如可以使用 gson 来完成转换,当然这种方式也得需要 gson 的相关

Spring MVC返回json视图时,如何将对象直接序列化成不带变量名做为根节点

Spring MVC返回json视图时,如何将对象直接序列化成不带变量名做为根节点的 json 报文 问题 问题描述起来比较拗口,其实就是用Spring MVC时,如何将对象映射成 json 报文时不把对象作为json的根节点.即使用@ResponseBody的效果. 比如,默认情况下,使用ModelAndView的addObject(key,object)或者ModelMap的addAttribute(key,object)保存完Java对象,然后交给Srping的视图解析器解析成json时,

ASP.NET MVC 4 中的JSON数据交互

前台Ajax请求很多时候需要从后台获取JSON格式数据,一般有以下方式: 拼接字符串 return Content("{\"id\":\"1\",\"name\":\"A\"}"); 为了严格符合Json数据格式,对双引号进行了转义. 使用JavaScriptSerialize.Serialize()方法将对象序列化为JSON格式的字符串 MSDN 例如我们有一个匿名对象: var tempObj=new

Spring MVC 返回类型为字符串时, 返回中文变成&quot;?&quot;处理

Spring MVC 返回类型为字符串时, 返回中文变成"?"处理 Spring controller 如下 @Controller public class SimpleController { @ResponseBody @RequestMapping(value = "/hotel") public String hotel() { return "{\"status\":0,\"errmsg\":null,\