简体   繁体   English

JAVA JACKSON:使用两个字段而不是所有类序列化一个类

[英]JAVA JACKSON: serialize a class with two field instead of all class

I need to serialize an entity with only two column when it's called by a foreign key. 当外键调用实体时,我需要序列化只有两列的实体。 I'am working in Wildfly, so I'am searching for a jackson solutions. 我在Wildfly工作,所以我正在寻找杰克逊解决方案。

Suppose I have entity class A 假设我有实体类A.

public class A{
   private Long id;
   private String name;
   private String anotherinfo;
   private String foo;
   ...
}

and another class B: 和另一个B类:

public class B{
   private Long id;
   private String name;
   private A parent;
}

I want to serialize A with all his field when i search for A, but when i need to retrieve an istance of B, i need only two field (an ID and a label) 当我搜索A时,我想用他的所有字段序列化A,但是当我需要检索B的istance时,我只需要两个字段(ID和标签)

If I use annotations: 如果我使用注释:

@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true)
private A parent;

i'll return only the id. 我只会返回id。

The result i want will be like: 我想要的结果将是:

B: {
  "id" : 1,
  "name" : "test",
  "parent" : {
     "id" : 1,
     "name" : 2
  }
}

You can use the JsonIgnoreProperties annotation, to disable specific fields for serialization (and deserialization): 您可以使用JsonIgnoreProperties批注来禁用序列化(和反序列化)的特定字段:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

public class B {
    private Long id;
    private String name;

    @JsonIgnoreProperties({"anotherinfo", "foo"})
    private A parent;

Have A extend another class, say C : A扩展另一个类,比如C

class C {
   Long id;
   String name;
}

class A extends C {
   String anotherinfo;
   String foo;
   ...
}

Then, in B : 然后,在B

class B {
   Long id;
   String name;
   @JsonSerialize(as=C.class)
   A parent;
}

When you serialize B , its parent field will have just the fields from C , but everywhere else that you serialize an A object you will see all the fields from both A and C . 序列化Bparent字段将只包含C的字段,但在序列化A对象的任何其他位置,您将看到AC所有字段。

For more information, take a look at https://github.com/FasterXML/jackson-annotations#annotations-for-choosing-moreless-specific-types 有关更多信息,请查看https://github.com/FasterXML/jackson-annotations#annotations-for-choosing-moreless-specific-types

Solved adding a Json Serializer. 解决了添加Json Serializer的问题。

I have created an NationJsonSerializer for the parent class: 我为父类创建了一个NationJsonSerializer:

public class NationJsonSerializer extends JsonSerializer<TNation> {

@Override
public void serialize(TNation value, JsonGenerator jgen, SerializerProvider provider) 
  throws IOException, JsonProcessingException {
    jgen.writeStartObject();
    jgen.writeNumberField("id", value.getId());
    jgen.writeStringField("name", value.getComune());
    jgen.writeStringField("iso", value.getCap());
    jgen.writeEndObject();
}
}

Then,in the city class, i put the annotation 然后,在城市课上,我把注释

@JoinColumn(name = "idtnation",referencedColumnName = "id",nullable = true)
@ManyToOne(targetEntity = TNation.class, fetch = FetchType.EAGER)
@JsonSerialize(using = NationJsonSerializer.class)
private TNation nation;

So, if I use a method Nation n = getNation(long id); 所以,如果我使用方法Nation n = getNation(long id); i'll receive all columns, but if i use getCity(), I'll receive a simplified version. 我将收到所有列,但如果我使用getCity(),我将收到一个简化版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM