在网络项目中,我使用hiberante和jersey,后者又与Jackson合作。对于使用hiberante的工作,我有3个实体类,它们反映了数据库表的结构,并且具有“一对多”和“多对一”的关系。绑定字段标注为@OneToMany
和@ManyToOne
。这些类的对象必须在json中序列化。为了避免序列化期间的错误,绑定字段也注释为@JsonManagedReference
和@JsonBackReference
。
由于Shops
类的对象的序列化,这样的行结果如下:
{
"id": 2,
"name": "Shop #2",
"category": "auto",
"distance": 120,
"discounts": [
{
"id": 9,
"title": "-50%",
"startDate": 1425679200000,
"endDate": 1425679200000
}
]
}
它不适合我,我决定编写自定义序列化程序。结果我想看到以下json:
{
"id": 2,
"name": "Shop #2",
"category": "auto",
"distance": 120,
"locality_id": 10,
}
为此,我为每个实体创建了3个类序列化程序,并清除了早先的注释@JsonManagedReference
和@JsonBackReference
,并添加了@JsonSerialize
关于类序列化程序的指示。
作为开始尝试的结果,我收到以下错误:
com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: java.util.ArrayList[0])
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:210)
at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:189)
at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:216)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:117)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:73)
at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:19)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1052)
我如何更正此错误?
实体类和类序列化器如下所示。
实体类:
地区
@XmlRootElement
@Entity
@JsonSerialize(using = LocalitiesSerializer.class)
@Table(name = "localities")
public class Localities {
@Id
@GeneratedValue
@Column(name="id", unique=true, nullable=false)
private Integer id;
@Column(name="name",nullable=false, length=57)
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "locality")
private Set<Shops> shops = new HashSet<Shops>(0);
//constructors, getters & setters
}
商店
@XmlRootElement
@Entity
@JsonSerialize(using = ShopsSerializer.class)
@Table(name = "shops")
public class Shops {
@Id
@GeneratedValue
@Column(name="id", unique=true, nullable=false)
protected Integer id;
@Column(name="name", length=45, nullable=false)
protected String name;
@Column(name="category", columnDefinition="ENUM('undefined','auto','children_prod','food','game','book','electronics','beuty_and_health','fashion','footwear','clothing','sports','homewere','pet_prod','services','gift_and_flowers')",
nullable=false)
@Enumerated(EnumType.STRING)
protected Categories category;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "locality_id", nullable = false)
protected Localities locality;
@Transient
private Double distance;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "shop")
private Set<Discounts> discounts = new HashSet<Discounts>(0);
//constructors, getters & setters
}
折扣
@XmlRootElement
@Entity
@JsonSerialize(using = DiscountsSerializer.class)
@Table(name="discounts")
public class Discounts {
@Id
@GeneratedValue
@Column(name="id")
private Integer id;
@Column(name="title")
private String title;
@Column(name="start_date")
@Temporal(TemporalType.DATE)
private Calendar startDate;
@Column(name="end_date")
@Temporal(TemporalType.DATE)
private Calendar endDate;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "shop_id", nullable = false)
private Shops shop;
//constructors, getters & setters
}
串行器:
LocalitiesSerializer
public class LocalitiesSerializer extends JsonSerializer<Localities> {
@Override
public void serialize(Localities locality, JsonGenerator jsonGen,
SerializerProvider serProv) throws IOException,
JsonProcessingException {
jsonGen.writeStartObject();
jsonGen.writeNumberField("id", locality.getId());
jsonGen.writeStringField("name", locality.getName());
jsonGen.writeEndObject();
}
}
ShopsSerializer
public class ShopsSerializer extends JsonSerializer<Shops> {
@Override
public void serialize(Shops shop, JsonGenerator jsonGen,
SerializerProvider serProv) throws IOException,
JsonProcessingException {
jsonGen.writeStartObject();
jsonGen.writeNumberField("id", shop.getId());
jsonGen.writeStringField("name", shop.getName());
jsonGen.writeStringField("category", shop.getCategory().toString());
jsonGen.writeNumberField("distance", shop.getDistance());
jsonGen.writeNumberField("locality_id", shop.getLocality().getId());
jsonGen.writeEndObject();
}
}
DiscountsSerializer
public class DiscountsSerializer extends JsonSerializer<Discounts> {
@Override
public void serialize(Discounts discount, JsonGenerator jsonGen,
SerializerProvider serProv) throws IOException,
JsonProcessingException {
jsonGen.writeStartObject();
jsonGen.writeNumberField("id", discount.getId());
jsonGen.writeStringField("title", discount.getTitle());
jsonGen.writeStringField("start_date", discount.getStartDate().toString());
jsonGen.writeStringField("end_date", discount.getEndDate().toString());
jsonGen.writeNumberField("shop_id", discount.getShop().getId());
jsonGen.writeEndObject();
}
}
P.S。在这种情况下,注释@JsonIgnore
不会进一步接近我计划清除注释@JsonSerialize
并使用类序列化程序,如下所示:
ObjectMapper mapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
module.addSerializer(Item.class, new ShopsSerializer());
mapper.registerModule(module);
String serialized = mapper.writeValueAsString(shop);
因此,有机会使用不同的序列化器。
答案 0 :(得分:1)
仅供参考(因为这是我正在寻找的,但最终在这里): Jackson - serialization of entities with birectional relationships (avoiding cycles)