简体   繁体   English

如何使用杰克逊注释(或任何其他方式)在反序列化中获取复杂数据?

[英]How to use Jackson annotations( or any other way)to get a complex data in the deserialization?

I use Entity Models with json serialization/deserialization to store/retrieve data . 我使用带有json序列化/反序列化的实体模型来存储/检索数据。 I face issue to deserialize Lazy loaded data. 我遇到了反序列化惰性加载数据的问题。

That is, I have defined my entity,superclass, interface like this; 也就是说,我已经定义了我的实体,超类,接口;

ATG Class ATG等级

@Entity
@Table(name = "atg", uniqueConstraints = @UniqueConstraint(columnNames = "code"))
public class ATG extends ParentEntity {

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "companyid")
    Manufacturer manufacturer;

    getManufacturer();
    setManufacturer();    
}

Customer class 客户等级

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Customer implements HasCustomer {

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
    @JoinColumn(name = "customer2_id", nullable = true)
    private Customer customer;

    getCustomer();
    setCustomer();
}

ParentEntity 上级实体

@MappedSuperclass
public abstract class ParentEntity implements  HasCustomer{

    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    @ManyToOne( fetch = FetchType.LAZY)
    @JoinColumn(name = "customer2_id", nullable = false)
    private Customer customer;

    getCustomer();
    setCustomer();
}

HasCustomer Interface HasCustomer界面

public interface HasCustomer extends Serializable{

    public Customer getCustomer();

    public void setCustomer(final Customer customer);

    public Long getCustomerId();

    public void setCustomerId(final Long customerId);
}

Customer Class has a reference to customer object(logical requirement). 客户类别参考了客户对象(逻辑要求)。

I make REST calls to my service endpoints to retrieve data from the database. 我对服务端点进行REST调用以从数据库中检索数据。 Ide fined those complex dataTypes as fetch = FetchType.LAZY. Ide对那些复杂的dataTypes进行了罚款,如fetch = FetchType.LAZY。 When user really requires those data with the response , he will specify. 当用户确实需要带有响应的数据时,他会指定。 So, at the backend we fetch data by setting FetchMode.JOIN. 因此,在后端,我们通过设置FetchMode.JOIN来获取数据。 (This works). (这有效)。

Only problem is, Json annotations. 唯一的问题是,Json批注。 How should i specify annotations in this case? 在这种情况下,我应该如何指定注释? If I keep jsonIgnore at one level, it may block results or I see results all the time when user is not requested to fetch the complex data 如果我将jsonIgnore保持在一个级别,则它可能会阻止结果,或者在不要求用户提取复杂数据时一直查看结果

I tried @jsonIgnore / @jsonproperty annotations on all getters, setters and the in the variable definition. 我在所有getter,setter和变量定义中尝试了@jsonIgnore / @jsonproperty批注。 (I believe I tried all combinations :) (我相信我尝试了所有组合:)

But none is returning the desired results. 但是没有人返回预期的结果。

I want to keep proper annotation to retrieve customer data and manufacturer data when making calls to ATG service 拨打ATG服务时,我想保留适当的注释以检索客户数据和制造商数据

Can anybody help me out? 有人可以帮我吗?

I use jackson(com.fasterxml.jackson) V 2.6.3 hibernate v 5.2.1.Final. 我使用jackson(com.fasterxml.jackson)V 2.6.3休眠v 5.2.1.Final。

I'm not sure you should be using Jackson like this, but this may help. 我不确定您是否应该像这样使用Jackson,但这可能会有所帮助。

Define a customer serializer 定义客户序列化器

public class CustomerSerializer extends JsonDeserializer<Customer> {

   public Customer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        ....
    }
}

Then use it in your interface 然后在您的界面中使用它

@JsonDeserialize(using = CustomerSerializer.class)
public class Customer implements HasCustomer {

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

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