选择并取消选择参数作为输出到JSON,具体取决于java中的传入请求

时间:2012-09-03 07:15:43

标签: java

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

对于路径变量{action},我可以有两个值,即{action}: 1)list 2)tile

我的带有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 = list 的参数private List space = new ArrayList(),而是显示 action = tile

如果有任何身体可以帮助我,那将会有很大的帮助。

2 个答案:

答案 0 :(得分:0)

如果类被序列化,并且您希望将space从序列化排除到JSON,则可以将其标记为transient

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

如果您想确定是否发生这种情况,您可以继承Area 因此,您的控制器可以调用ListAreaTileArea

答案 1 :(得分:0)

我得到了我的问题的答案

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

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

<强> @JsonSerialize(包括= JsonSerialize.Inclusion.NON_NULL) 公共课区{

现在value==null不会包含在序列化输出中的参数,但我可以使用constructur动态初始化它,并在需要时重新输出。