Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 删除由@JsonUnwrapped-RestController引起的json中的重复键_Java_Spring Mvc_Jackson - Fatal编程技术网

Java 删除由@JsonUnwrapped-RestController引起的json中的重复键

Java 删除由@JsonUnwrapped-RestController引起的json中的重复键,java,spring-mvc,jackson,Java,Spring Mvc,Jackson,我有一个POJO持有另一个POJO: public class Outer { private Long id; private String name; @JsonUnwrapped private Inner inner; } 这是内部类: public class Inner { private Long id; private String name; private String feature; } 正如您所看到的

我有一个POJO持有另一个POJO:

public class Outer {

    private Long id;
    private String name;

    @JsonUnwrapped
    private Inner inner;

}
这是内部类:

public class Inner {

    private Long id;
    private String name;
    private String feature;

}
正如您所看到的,这两个对象可能都有id和name,因此,尽管我使用了
@JsonUnwrapped
,但可能存在一些重复的键。 我有一个休息控制器:

@RestController
public class Temp {

    @RequestMapping("/test")
    public Outer test() {
        Inner inner = new Inner(null, "innerName", "feature");
        Outer outer = new Outer(10L, "outerName", inner);
        return outer;
    }

}
无论何时调用该方法,结果如下:
{“id”:1,名称:“outerName”,id:null,名称:“innerName”,“feature”:“feature”}

我想得到这个结果:

{“id”:1,名称:“innerName”,“feature”:“feature”}


我的意思是,每当内部对象具有重复键的值时,应该从内部对象中提取该值,如果内部类中重复键的值为null,则应该从外部对象中提取该值。

如果业务逻辑需要此类结构,然后我建议使用一个单独的类进行序列化(这通常是个好主意)

编辑:我不是杰克逊方面的专家,但我认为单凭杰克逊的注解是不可能做到的。有这样的库可以简化这样的操作。我相信它也应该能够满足你的需要


另一种方法是添加实现逻辑的getter方法:

public class Outer {
    @JsonIgnore
    private Long id;

    @JsonIgnore
    private String name;

    @JsonIgnore
    private Inner inner;

    @JsonProperty("id")
    public int getJsonId() {
       return inner == null || inner.getId() == null ? id : inner.getId();
    }
}

如果业务逻辑需要这种类结构,那么我建议使用单独的类进行序列化(这通常是个好主意)

编辑:我不是杰克逊方面的专家,但我认为单凭杰克逊的注解是不可能做到的。有这样的库可以简化这样的操作。我相信它也应该能够满足你的需要


另一种方法是添加实现逻辑的getter方法:

public class Outer {
    @JsonIgnore
    private Long id;

    @JsonIgnore
    private String name;

    @JsonIgnore
    private Inner inner;

    @JsonProperty("id")
    public int getJsonId() {
       return inner == null || inner.getId() == null ? id : inner.getId();
    }
}
你可以这样做:

public class Outer {
    private Long id;
    private String name;

    @JsonUnwrapped
    @JsonIgnoreProperties({"id", "name"})
    private Inner inner;
}
你可以这样做:

public class Outer {
    private Long id;
    private String name;

    @JsonUnwrapped
    @JsonIgnoreProperties({"id", "name"})
    private Inner inner;
}

使用
@JsonUnwrapped
时,
内部
将覆盖
外部
相同的键

所以这不会发生。
{“id”:1,name:“outerName”,“id”:null,“name:“innerName”,“feature:“feature”}
{“id”:null,“name:“innerName”,“feature:“feature”}
是正确的

在本例中,我们希望获得
{“id”:1,“name”:“innerName”,“feature”:“feature”}

内部
需要更改

公共类内部{
@JsonInclude(JsonInclude.Include.NON_NULL)
私人长id;
私有字符串名称;
私有字符串特征;
}

@JsonInclude(JsonInclude.Include.NON_NULL)
可以忽略空值。要实现使用
外部
id

时使用
@JsonUnwrapped
内部
将覆盖
外部
相同的键

所以这不会发生。
{“id”:1,name:“outerName”,“id”:null,“name:“innerName”,“feature:“feature”}
{“id”:null,“name:“innerName”,“feature:“feature”}
是正确的

在本例中,我们希望获得
{“id”:1,“name”:“innerName”,“feature”:“feature”}

内部
需要更改

公共类内部{
@JsonInclude(JsonInclude.Include.NON_NULL)
私人长id;
私有字符串名称;
私有字符串特征;
}

@JsonInclude(JsonInclude.Include.NON_NULL)
可以忽略空值。要使用
外部
id
请尝试下面的pojo。我想你的问题会解决的

public class Outer {

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long id;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;
    @JsonUnwrapped
    private Inner inner;
}

public class Inner {

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long id;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;
    private String feature;
}

试试下面的pojo。我想你的问题会解决的

public class Outer {

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long id;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;
    @JsonUnwrapped
    private Inner inner;
}

public class Inner {

    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long id;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;
    private String feature;
}

当内部对象具有复制键的值时,我的意思是什么?这里什么是复制键?id和name是复制键,因为它们同时存在于内部对象和外部对象中。我的意思是,当内部对象有一个复制键的值时?这里什么是重复键?id和名称是重复键,因为它们存在于内部对象和外部对象中感谢你的美丽想法,但问题是我有很多这样的域模型,如果我想为所有这些模型都做,那将是丑陋的!我正在寻找Jackson的一些配置(如果存在的话!)@DanielSmith我已经用更多的想法更新了我的答案。谢谢你美丽的想法,但问题是我有很多这样的领域模型,如果我想为所有的领域模型都这样做,那就太难看了!我正在寻找Jackson的一些配置(如果存在!)@DanielSmith我已经用更多的想法更新了我的答案。谢谢你的答案,但我认为这不是一个好主意,因为如果outer.name为null,那么我就失去了inner.name。谢谢你的答案,但我认为这不是一个好主意,因为如果outer.name为null,然后我丢失了inner.name。不幸的是,它会发生:{“id”:1,name:“outerName”,“id”:null,“name:“innerName”,“feature:“feature”}不幸的是,它会发生:{“id”:1,name:“outerName”,“id”:null,“name:“innerName”,“feature:“feature”}