使用ArrayLists的@ResponseBody序列化错误

时间:2011-09-06 19:09:59

标签: java serialization spring-mvc arraylist jackson

我尝试使用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;
    }

}

1 个答案:

答案 0 :(得分:3)

关键信息是and no properties discovered to create BeanSerializer:您的班级ResourceResultSetColResourceResultSetRow应该拥有默认的公共构造函数和所有属性的getter / setter。