使用 ArrayLists 的 @ResponseBody 序列化错误

Bil*_*ote 1 java serialization arraylist spring-mvc 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])

图结果集.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);
    }

}
Run Code Online (Sandbox Code Playgroud)

资源结果集.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)));
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

ResourceResultSetRow.java:

public class ResourceResultSetRow implements Serializable {

    private String timestamp;
    private int result;

    ResourceResultSetRow(String timestamp, int result) {
        this.timestamp = timestamp;
        this.result = result;
    }

}
Run Code Online (Sandbox Code Playgroud)

dma*_*a_k 6

关键的信息是and no properties discovered to create BeanSerializer:你的类ResourceResultSetCol,并ResourceResultSetRow应该对所有的属性默认的公共构造函数和getter / setter方法。