尝试使用 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])
图形结果集.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);
}
}
资源结果集.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;
}
}
最佳答案
关键信息是并且没有发现创建 BeanSerializer 的属性
:您的类 ResourceResultSetCol
和 ResourceResultSetRow
应该有默认的公共(public)构造函数和 getters/setters对于所有属性。
关于java - @ResponseBody 使用 ArrayLists 序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7324872/