java - @ResponseBody 使用 ArrayLists 序列化错误

标签 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])

图形结果集.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 的属性:您的类 ResourceResultSetColResourceResultSetRow 应该有默认的公共(public)构造函数和 getters/setters对于所有属性。

关于java - @ResponseBody 使用 ArrayLists 序列化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7324872/

相关文章:

java - 如何在 jTable 中显示二维数组?

c# - 结合序列化和语法解析的方法?

java - Spring MVC - 无法获取RequestDispatcher

org/springframework/aop/framework/AbstractAdvisingBeanPostProcessor 的 java.lang.NoClassDefFoundError

java JNLP错误: javax.net.ssl.SSLHandshakeException

java itext捕获空异常pdf文本提取

java - 尝试在 OfBiz 中保存任务时收到 FK 违规错误

python - 在 Python 和 Marshmallow 中嵌套数据库中不存在的字段

java - 如何在 Hazelcast 中为所有自定义类型配置自定义序列化器

java - 使用 spring mvc 下载的 Xlsx 文件已损坏且内容类型已更改