使用 ArrayList 的 @ResponseBody 序列化错误
我尝试使用 SpringMVC @ResponseBody 将我的对象作为 JSON 字符串返回时收到以下错误:
org.codehaus.jackson.map.JsonMappingException:找不到类 com.ResourceResultSetCol 的序列化程序,并且没有发现创建 BeanSerializer 的属性(为了避免异常,禁用 SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) )(通过参考链: com.medplus.devops.pdt.server.ResourceResultSet["cols"]->java.util.ArrayList[0])
GraphResultSet.java:
@Controller
@RequestMapping("/pdt")
public class GraphResultSet {
@RequestMapping(value = "/getResourceResultSet", method = RequestMethod.GET)
public @ResponseBody
ResourceResultSet getResourceResultSet(
@RequestParam(value = "resourceId", required = true) int resourceId) {
return new ResourceResultSet(resourceId);
}
}
ResourceResultSet.java:
public class ResourceResultSet implements Serializable {
public String resourceName;
public ArrayList<ResourceResultSetCol> cols;
public ArrayList<ResourceResultSetRow> rows;
ResourceResultSet(int id) {
resourceName = "Graph " + id;
cols = new ArrayList<ResourceResultSetCol>();
cols.add(new ResourceResultSetCol("col1","Timestamp","date"));
cols.add(new ResourceResultSetCol("col2","Value","number"));
int randomNumberOfResults = new Random().nextInt(5);
int numberOfResults[] = new int[] {12,288,2016,8928,107136}; // hour, day, week, month, year
Calendar now = Calendar.getInstance();
rows = new ArrayList<ResourceResultSetRow>();
for (int resultIndex = 0; resultIndex <= numberOfResults[randomNumberOfResults]; ++resultIndex) {
now.setTime(now.getTime());
now.add(Calendar.MINUTE, resultIndex * -5);
this.rows.add(new ResourceResultSetRow(now.getTime().toString(), new Random().nextInt(101)));
}
}
}
ResourceResultSetCol.java:
public class ResourceResultSetCol implements Serializable {
private String id;
private String label;
private String type;
public ResourceResultSetCol(String id, String label, String type){
this.id = id;
this.label = label;
this.type = type;
}
}
ResourceResultSetRow.java:
public class ResourceResultSetRow implements Serializable {
private String timestamp;
private int result;
ResourceResultSetRow(String timestamp, int result) {
this.timestamp = timestamp;
this.result = result;
}
}
I am getting the following error trying to return my object as a JSON string using SpringMVC @ResponseBody:
org.codehaus.jackson.map.JsonMappingException: No serializer found for class com.ResourceResultSetCol and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.medplus.devops.pdt.server.ResourceResultSet["cols"]->java.util.ArrayList[0])
GraphResultSet.java:
@Controller
@RequestMapping("/pdt")
public class GraphResultSet {
@RequestMapping(value = "/getResourceResultSet", method = RequestMethod.GET)
public @ResponseBody
ResourceResultSet getResourceResultSet(
@RequestParam(value = "resourceId", required = true) int resourceId) {
return new ResourceResultSet(resourceId);
}
}
ResourceResultSet.java:
public class ResourceResultSet implements Serializable {
public String resourceName;
public ArrayList<ResourceResultSetCol> cols;
public ArrayList<ResourceResultSetRow> rows;
ResourceResultSet(int id) {
resourceName = "Graph " + id;
cols = new ArrayList<ResourceResultSetCol>();
cols.add(new ResourceResultSetCol("col1","Timestamp","date"));
cols.add(new ResourceResultSetCol("col2","Value","number"));
int randomNumberOfResults = new Random().nextInt(5);
int numberOfResults[] = new int[] {12,288,2016,8928,107136}; // hour, day, week, month, year
Calendar now = Calendar.getInstance();
rows = new ArrayList<ResourceResultSetRow>();
for (int resultIndex = 0; resultIndex <= numberOfResults[randomNumberOfResults]; ++resultIndex) {
now.setTime(now.getTime());
now.add(Calendar.MINUTE, resultIndex * -5);
this.rows.add(new ResourceResultSetRow(now.getTime().toString(), new Random().nextInt(101)));
}
}
}
ResourceResultSetCol.java:
public class ResourceResultSetCol implements Serializable {
private String id;
private String label;
private String type;
public ResourceResultSetCol(String id, String label, String type){
this.id = id;
this.label = label;
this.type = type;
}
}
ResourceResultSetRow.java:
public class ResourceResultSetRow implements Serializable {
private String timestamp;
private int result;
ResourceResultSetRow(String timestamp, int result) {
this.timestamp = timestamp;
this.result = result;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关键消息是
并且没有发现创建 BeanSerializer 的属性
:您的类ResourceResultSetCol
和ResourceResultSetRow
应该具有所有属性的默认公共构造函数和 getters/setters 。The key message is
and no properties discovered to create BeanSerializer
: your classesResourceResultSetCol
andResourceResultSetRow
should have default public constructor and getters/setters for all properties.