![](/img/trans.png)
[英]Is there any jackson json strategy (using annotations or another way), that will execute some logic prior and post deserialization of field?
[英]How to use Jackson annotations( or any other way)to get a complex data in the deserialization?
我使用带有json序列化/反序列化的实体模型来存储/检索数据。 我遇到了反序列化惰性加载数据的问题。
也就是说,我已经定义了我的实体,超类,接口;
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();
}
客户等级
@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();
}
上级实体
@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界面
public interface HasCustomer extends Serializable{
public Customer getCustomer();
public void setCustomer(final Customer customer);
public Long getCustomerId();
public void setCustomerId(final Long customerId);
}
客户类别参考了客户对象(逻辑要求)。
我对服务端点进行REST调用以从数据库中检索数据。 Ide对那些复杂的dataTypes进行了罚款,如fetch = FetchType.LAZY。 当用户确实需要带有响应的数据时,他会指定。 因此,在后端,我们通过设置FetchMode.JOIN来获取数据。 (这有效)。
唯一的问题是,Json批注。 在这种情况下,我应该如何指定注释? 如果我将jsonIgnore保持在一个级别,则它可能会阻止结果,或者在不要求用户提取复杂数据时一直查看结果
我在所有getter,setter和变量定义中尝试了@jsonIgnore
/ @jsonproperty
批注。 (我相信我尝试了所有组合:)
但是没有人返回预期的结果。
拨打ATG服务时,我想保留适当的注释以检索客户数据和制造商数据
有人可以帮我吗?
我使用jackson(com.fasterxml.jackson)V 2.6.3休眠v 5.2.1.Final。
我不确定您是否应该像这样使用Jackson,但这可能会有所帮助。
定义客户序列化器
public class CustomerSerializer extends JsonDeserializer<Customer> {
public Customer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
....
}
}
然后在您的界面中使用它
@JsonDeserialize(using = CustomerSerializer.class)
public class Customer implements HasCustomer {
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.