java循环引用jackson_java-处理Jackson亲子序列化

我在项目中使用Spring MVC(这是一个新功能),Hibernate和Jackson,并且在服务器和客户端之间使用JSON进行数据交换.

我有一对具有ManyToOne / OneToMany关系的“雇员”和“地址”类.

我遇到了循环引用错误,但是,我能够使用@JsonManagedReference和@JsonBackReference解决该问题.

但是问题是,在序列化过程中(我正在查询数据库以获取所有雇员),Jackson完全忽略了Address属性,仅序列化了3个字段(其他字段已被特别忽略,您可以在代码中看到).

这是返回的JSON

[

{

"id": 1,

"name": "xxx",

"age": 100

},

{

"id": 2,

"name": "yyy",

"age": 100

}

]

员工类别:

@Entity

@Table(name = "e_employee", catalog = "emploman")

public class Employee implements java.io.Serializable {

private int id;

private String name;

private int age;

private Address address;

private String modifiedBy;

private Date modifiedTime;

private transient int addressId;

public Employee() {

}

public Employee(int id, String name, int age, Address address, String modifiedBy, Date modifiedTime) {

this.id = id;

this.name = name;

this.age = age;

this.address = address;

this.modifiedBy = modifiedBy;

this.modifiedTime = modifiedTime;

}

public Employee(String name, int age, Address address, String modifiedBy, Date modifiedTime) {

this.name = name;

this.age = age;

this.address = address;

this.modifiedBy = modifiedBy;

this.modifiedTime = modifiedTime;

}

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name = "id", unique = true, nullable = false)

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Column(name = "name", nullable = false, length = 100)

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

@Column(name = "age", nullable = false)

public int getAge() {

return age;

}

public void setAge(int age) {

this.age = age;

}

@ManyToOne(fetch = FetchType.LAZY)

@JoinColumn(name = "address", nullable = false)

@JsonBackReference("employee-address")

public Address getAddress() {

return address;

}

public void setAddress(Address address) {

this.address = address;

}

@Column(name = "modified_by", nullable = false, length = 50)

@JsonIgnore

public String getModifiedBy() {

return modifiedBy;

}

@JsonIgnore

public void setModifiedBy(String modifiedBy) {

this.modifiedBy = modifiedBy;

}

@Temporal(TemporalType.TIMESTAMP)

@Column(name = "modified_time", nullable = false, length = 19)

@JsonIgnore

public Date getModifiedTime() {

return modifiedTime;

}

@JsonIgnore

public void setModifiedTime(Date modifiedTime) {

this.modifiedTime = modifiedTime;

}

@JsonIgnore

@Transient

public int getAddressId() {

return addressId;

}

@JsonIgnore

public void setAddressId(int addressId) {

this.addressId = addressId;

}

}

地址类别:

@Entity

@Table(name = "e_address", catalog = "emploman")

public class Address implements java.io.Serializable {

private int id;

private String country;

private String state;

private String city;

private String streetAddress;

private String pinCode;

private String modifiedBy;

private Date modifiedTime;

private Set employees;

public Address() {

}

public Address(int id, String country, String state, String city, String streetAddress, String pinCode, String modifiedBy, Date modifiedTime, Set employees) {

this.id = id;

this.country = country;

this.state = state;

this.city = city;

this.streetAddress = streetAddress;

this.pinCode = pinCode;

this.modifiedBy = modifiedBy;

this.modifiedTime = modifiedTime;

this.employees = employees;

}

public Address(String country, String state, String city, String streetAddress, String pinCode, String modifiedBy, Date modifiedTime, Set employees) {

this.country = country;

this.state = state;

this.city = city;

this.streetAddress = streetAddress;

this.pinCode = pinCode;

this.modifiedBy = modifiedBy;

this.modifiedTime = modifiedTime;

this.employees = employees;

}

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

@Column(name = "id")

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

@Column(name = "country", nullable = false, length = 100)

public String getCountry() {

return country;

}

public void setCountry(String country) {

this.country = country;

}

@Column(name = "state", nullable = false, length = 100)

public String getState() {

return state;

}

public void setState(String state) {

this.state = state;

}

@Column(name = "city", nullable = false, length = 100)

public String getCity() {

return city;

}

public void setCity(String city) {

this.city = city;

}

@Column(name = "street_address", nullable = false, length = 500)

public String getStreetAddress() {

return streetAddress;

}

public void setStreetAddress(String streetAddress) {

this.streetAddress = streetAddress;

}

@Column(name = "pincode", nullable = false, length = 15)

public String getPinCode() {

return pinCode;

}

public void setPinCode(String pinCode) {

this.pinCode = pinCode;

}

@Column(name = "modified_by", nullable = false, length = 50)

public String getModifiedBy() {

return modifiedBy;

}

public void setModifiedBy(String modifiedBy) {

this.modifiedBy = modifiedBy;

}

@Temporal(TemporalType.TIMESTAMP)

@Column(name = "modified_time", nullable = false, length = 19)

public Date getModifiedTime() {

return modifiedTime;

}

public void setModifiedTime(Date modifiedTime) {

this.modifiedTime = modifiedTime;

}

@OneToMany(mappedBy = "address", fetch = FetchType.LAZY)

@JsonManagedReference(value = "employee-address")

public Set getEmployees() {

return employees;

}

public void setEmployees(Set employees) {

this.employees = employees;

}

}

因此,基本上,我期望的json响应如下所示:

[

{

"id": 1,

"name": "xxx",

"age": 100,

"address": {

"country": "xxx",

"city": "abc"

}

}

]

谁能帮忙吗?

更新1

我尝试从数据库获取地址,并且成功获取了与该地址相关的员工

[

{

"id": 1,

"country": "xxx",

"state": "yyy",

"city": "zzz",

"streetAddress": "abc",

"pinCode": "12345",

"modifiedBy": "xxx",

"modifiedTime": 1400930509000,

"employees": [

{

"id": 2,

"name": "xxx",

"age": 190

},

{

"id": 1,

"name": "xxx",

"age": 200

}

]

}

]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 Jackson 库删除 JSON 元素的过程如下: 1. 将 JSON 字符串转换为 JsonNode 对象。可以使用 ObjectMapper 类的 readTree() 方法来实现。例如: ``` ObjectMapper objectMapper = new ObjectMapper(); JsonNode rootNode = objectMapper.readTree(jsonString); ``` 这里的 `jsonString` 是包含 JSON 数据的字符串。 2. 找到要删除的元素,并将其删除。可以使用 JsonNode 的 findValue() 方法或者 findPath() 方法来获取元素,再使用 removeAll() 方法将其删除。例如: ``` JsonNode nodeToRemove = rootNode.findValue("elementName"); ((ObjectNode)nodeToRemove.getParent()).removeAll(); ``` 这里的 `"elementName"` 是需要删除的元素名称。`findValue()` 方法返回的是包含该元素的 JsonNode 对象,`getParent()` 方法返回的是该 JsonNode 对象的父节点,`removeAll()` 方法将该 JsonNode 对象及其子节点全部删除。 3. 最后,将修改后的 JsonNode 对象转换回 JSON 字符串。可以使用 ObjectMapper 类的 writeValueAsString() 方法来实现。例如: ``` String newJsonString = objectMapper.writeValueAsString(rootNode); ``` 这里的 `newJsonString` 是删除元素后得到的新的 JSON 字符串。 需要注意的是,如果要删除的元素在 JSON 中存在多个,上述代码只会删除其中一个。如果需要删除所有匹配的元素,可以将上述代码放在一个循环中,直到所有匹配的元素全部删除为止。 另外,如果出现内存溢出的问题,可能是因为 JSON 数据量太大,导致 JsonNode 对象占用的内存过高。可以考虑使用流式 API(Streaming API)来解析 JSON 数据,以降低内存消耗。需要注意的是,使用流式 API 会牺牲一定的性能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值