Jackson JSON序列化程序返回额外的集合名称

时间:2016-03-10 04:28:35

标签: java json jackson

我列出了作为杰克逊列表返回的内容。我想要的是这个:

"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);

}

}

Thing Entity

@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<>();

1 个答案:

答案 0 :(得分:0)

为什么不使用ObjectMapper来序列化和反序列化数据?

看看这个小测试,我相信它能做到你想要的:

@Test
public void myTest() throws Exception{
    ObjectMapper mapper = new ObjectMapper();
    mapper.setVisibility(
            mapper.getSerializationConfig().
            getDefaultVisibilityChecker().
            withFieldVisibility(JsonAutoDetect.Visibility.ANY).
            withGetterVisibility(JsonAutoDetect.Visibility.NONE).
            withSetterVisibility(JsonAutoDetect.Visibility.NONE).
            withCreatorVisibility(JsonAutoDetect.Visibility.NONE).
            withIsGetterVisibility(JsonAutoDetect.Visibility.NONE));
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);

    mapper.setSerializationInclusion(Include.NON_NULL);

    TestObject value = new TestObject();
    value.a = "TestAvalue";
    value.set = new HashSet<>();
    value.set.add(new SetValueObject(1, 1));
    value.set.add(new SetValueObject(2, 2));
    String test = mapper.writeValueAsString(value);
    System.out.println(test);
}


public class TestObject{
    String a;
    Set<SetValueObject> set;
}

public class SetValueObject{

    public SetValueObject(int a, int b){
        this.a = a;
        this.b = b;
    }


    int a;
    int b;
}

输出:

{"a":TestAvalue","set":[{"a":1,"b":1},{"a":2,"b":2}]}

使用Jackson 2.6.1进行测试

我修改了我的一个测试,所以我不确定你是否需要所有这个ObjectMapper config =&gt;它只是通过使用ObjectMapper让您了解另一种方法。