1

例如,我有一个带有 URL 的传入请求:“/cid/{cid}/{action}”

对于路径变量 {action},我可以有两个值,即 {action}:1)列表 2)平铺

我的带有 JSON 输出参数的 POJO 类如下:

public class Area {

    private Integer aId;
    private String aName;
    private Integer allSpaces;
    private Integer violated;
    private Integer percent;
    private Integer level;
    private List<Space> space = new ArrayList<Space>(); // this List should be depend on condition

    public Area(Integer aId, String aName, Integer allSpaces, Integer violated, Integer percent, Integer level) {
        this.aId = aId;
        this.aName = aName;
        this.allSpaces = allSpaces;
        this.level = level;
        this.violated = violated;
        this.percent = percent;
    }

    public Integer getaId() {
        return aId;
    }

    public void setaId(Integer aId) {
        this.aId = aId;
    }

    public String getaName() {
        return aName;
    }

    public void setaName(String aName) {
        this.aName = aName;
    }

    public Area addSpace(Space s) {
        space.add(s);

        return this;
    }

    public List<Space> getSpace() {
        return space;
    }

    public void setSpace(List<Space> space) {
        this.space = space;
    }

    public Integer getAllSpaces() {
        return allSpaces;
    }

    public void setAllSpaces(Integer allSpaces) {
        this.allSpaces = allSpaces;
    }

    public Integer getViolated() {
        return violated;
    }

    public void setViolated(Integer violated) {
        this.violated = violated;
    }

    public Integer getPercent() {
        return percent;
    }

    public void setPercent(Integer percent) {
        this.percent = percent;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }
}

我的输出不应该包含action =listprivate List space = new ArrayList()的参数,而是显示action=tile的参数。

如果有任何机构可以帮助我,这将有很大帮助。

4

2 回答 2

0

如果该类被序列化,并且您想space从序列化中排除为 JSON,则可以将其标记为transient.

private transient List<Space> space = new ArrayList<Space>();

如果您想决定是否发生这种情况,您可以子类化Area 所以您的控制器将调用ListAreaTileArea

于 2012-09-03T07:25:47.397 回答
0

我得到了我的问题的答案

我初始化private List<Space> space = null;而不是private List<Space> space = new ArrayList<Space>();

并在我的 pojo 顶部添加了@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) ,如下所示

@JsonSerialize(include=JsonSerialize.Inclusion.NON_NULL) 公共类区域 {

现在value==null将不包含在序列化输出中的参数,但我可以通过使用构造函数动态初始化它,并在需要时检索输出。

于 2012-09-04T10:18:02.360 回答