【发布时间】:2016-06-24 18:32:53
【问题描述】:
我有一个作为杰克逊列表返回的东西的列表。我想要的是这样的:
"things": { [
{
"id": "1234",
..学生名单
但目前,我得到了这个:
"things": {
"HashSet": [
{
"id": "1234",
我正在使用 JsonSerializer>,这就是它添加 HashSet 字段的原因。我尝试在该字段上添加一个 json 属性,但由于它是一个局部变量,因此不允许这样做。
我目前正在使用 jackson 库并已调查:
Jackson annotation - How to rename element names?
How to rename root key in JSON serialization with Jackson
但他们似乎有一个完全不同的问题。有任何想法吗?谢谢!
编辑:
添加了类实现。请注意,我称其为所有者,其中包含事物。此外,我的 jpa 注释也在那里。谢谢。
@Entity @Table(name = "owner")
public class Owner extends BaseEntity implements Persistence {
@Column
private String name;
@Column
private String description;
@Column
private Integer capacity;
@Column
@JsonSerialize(using = ThingSerializer.class)
@JsonProperty("things")
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "owners")
private Set<Thing> tihngs = new HashSet<>();
public class ThingSerializer extends JsonSerializer<Set<Thing>> {
@Override public void serialize(Set<Thing> thingSet, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws
IOException,
JsonProcessingException {
Set<Thing> things = new HashSet<>();
thingSet.forEach(thing -> {
Thing t = new Thing();
t.setCreatedBy(thing.getCreatedBy());
t.setCreationDate(thing.getCreationDate());
t.setId(thing.getId());
t.setDateModified(thing.getDateModified());
t.setModifiedBy(thing.getModifiedBy());
t.setStatus(thing.getStatus());
things.add(s);
});
jsonGenerator.writeObject(things);
}
}
事物实体
@Entity
@Table(name = "thing")
public class Thing extends BaseEntity implements Persistence {
private static final long serialVersionUID = 721739537425505668L;
private String createdBy;
private Date creationDate;
.
.
.
@OneToMany(fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
@JoinTable(name = "ThingOwner", joinColumns = @JoinColumn(name = "thing_id") , inverseJoinColumns = @JoinColumn(name = "owner_id") )
private Set<Owner> owners = new HashSet<>();
【问题讨论】:
-
你好,你能补充一些细节,比如学生类,序列化代码吗?