使用 ArrayList 的 @ResponseBody 序列化错误

发布于 2024-12-03 03:37:21 字数 2543 浏览 2 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2024-12-10 03:37:21

关键消息是并且没有发现创建 BeanSerializer 的属性:您的类 ResourceResultSetColResourceResultSetRow 应该具有所有属性的默认公共构造函数和 getters/setters 。

The key message is and no properties discovered to create BeanSerializer: your classes ResourceResultSetCol and ResourceResultSetRow should have default public constructor and getters/setters for all properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文