【问题标题】:Jackson returns wrong JSON杰克逊返回错误的 JSON
【发布时间】:2020-06-30 00:24:13
【问题描述】:

我正在尝试从我的 Postgres 数据库中返回一个路由列表。当我到达我的端点时,有 2 个实体的列表出来了,但是有一个问题,列表中的第二个元素没有他的原始对象作为第一个。 这是 JSON:

[
{
    "id": 1,
    "weekDay": "Terça-Feira",
    "travel": 1,
    "origin": {
        "id": 1,
        "placeName": "Prefeito Mario de Menezes, Avenida",
        "placeNumber": 1,
        "placeZone": 1,
        "geom": null,
        "x": -23.266556,
        "y": -51.038334,
        "lastUpdate": 1583809200000,
        "registerDate": 1583809200000,
        "disabled": false
    },
    "startTime": "08:00 - 08:29",
    "destiny": {
        "id": 2,
        "placeName": "Ronat Valter Sodré, Rua",
        "placeNumber": 2,
        "placeZone": 2,
        "geom": null,
        "x": -23.274357,
        "y": -51.060287,
        "lastUpdate": 1583809200000,
        "registerDate": 1583809200000,
        "disabled": false
    },
    "arrivalTime": "08:00 - 08:29",
    "morador": {
        "id": 1,
        "gender": "Masculino",
        "defic": false,
        "ageRange": "20 Anos",
        "schooling": "Graduado",
        "occupation": "Assalariado",
        "place": 1,
        "lastUpdate": 1583895600000,
        "registerDate": 1583895600000,
        "disabled": false
    },
    "reason": "Lazer",
    "travelMode": "Moto (como motorista)",
    "geom": null,
    "lastUpdate": 1584561952039,
    "registerDate": 1584414000000,
    "disabled": false
},
{
    "id": 475,
    "weekDay": "Quarta-Feira",
    "travel": 475,
    "origin": 1, // THIS NEED TO BE AN OBJECT OF "PLACE"!!!
    "startTime": "18:30 - 18:59",
    "destiny": {
        "id": 81,
        "placeName": "PR 090",
        "placeNumber": 81,
        "placeZone": 81,
        "geom": null,
        "x": -23.27377,
        "y": -51.030827,
        "lastUpdate": 1583809200000,
        "registerDate": 1583809200000,
        "disabled": false
    },
    "arrivalTime": "19:00 - 19:29",
    "morador": {
        "id": 1,
        "gender": "Masculino",
        "defic": false,
        "ageRange": "20 Anos",
        "schooling": "Graduado",
        "occupation": "Assalariado",
        "place": 1,
        "lastUpdate": 1583895600000,
        "registerDate": 1583895600000,
        "disabled": false
    },
    "reason": "Trabalho",
    "travelMode": "Carro (como motorista)",
    "geom": null,
    "lastUpdate": 1584554752290,
    "registerDate": 1584414000000,
    "disabled": false
}
]

我的模型是: 路线:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity(name = "od_routes")
@Where(clause = "disabled= false")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Route {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String weekDay;

    @Column
    private int travel;

    @ManyToOne(fetch = FetchType.LAZY)
    private Logradouro origin;

    @Column
    private String startTime;

    @ManyToOne(fetch = FetchType.LAZY)
    private Logradouro destiny;

    @Column
    private String arrivalTime;

    @ManyToOne(fetch = FetchType.LAZY)
    private Dweller dweller;

    @Column
    private String reason;

    @Column
    private String travelMode;

    @Column
    private Geometry geom;

    @Column
    private Date lastUpdate;

    @Column
    private Date registerDate;

    @Column
    private boolean disabled;
}

居民:

 @Entity(name = "od_dweller")
 @Where(clause = "disabled = false")
 @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
 public class Dweller {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String gender;

    @Column
    private boolean defic;

    @Column
    private String ageRange;

    @Column
    private String schooling;

    @Column
    private String occupation;

    @ManyToOne
    private Place place;

    @Column
    private Date lastUpdate;

    @Column
    private Date registerDate;

    @Column
    private boolean disabled;
}

地点:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@Entity(name = "od_places")
@Where(clause = "disabled = false")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class Place {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column
    private String placeName;

    @Column
    private int placeNumber;

    @Column
    private int placeZone;

    @Column
    private Geometry geom;

    @Column
    private float x;

    @Column
    private float y;

    @Column
    private Date lastUpdate;

    @Column
    private Date registerDate;

    @Column
    private boolean disabled;
}

我做错了吗? (代码是这样翻译的,如果有些名字是错误的,那只是翻译错误)

【问题讨论】:

    标签: json spring model-view-controller serialization jackson


    【解决方案1】:

    只需从您的 Place 类中删除 @JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")。我认为这不会破坏任何东西,因为没有对此类的循环引用

    您可以在此处阅读有关此行为的信息

    https://github.com/FasterXML/jackson-docs/wiki/Presentation-Jackson-2.0

    @JsonIdentityInfo 的工作方式如下:

    • 对象 ID 始终作为属性包含(具有可配置的名称)。这意味着输出形状必须是 JSON 对象;并且意味着 当前无法识别 Java 集合、数组和映射 处理过

    • Object的第一个实例被完全序列化(包括Object Id);进一步的引用使用对象 ID 本身进行序列化

    • 可以生成对象 ID(Jackson 动态创建 ID),作为 UUID 或序列号(整数);或提供,在这种情况下 对象本身通过属性(字段值或 吸气剂)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-02
      • 2018-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多