【问题标题】:Java Spring HttpMessageNotWritableException/JsonMappingException with Hibernate FetchType.LAZY in Jackson serialization processJackson序列化过程中带有Hibernate FetchType.LAZY的Java Spring HttpMessageNotWritableException/JsonMappingException
【发布时间】:2016-01-29 14:27:32
【问题描述】:

我使用Java Spring 网络控制器和jackson 来解析响应。我还有四个JavaHibernate实体:UserFilterMakeModel,在 Make -> Model(s) 中有双向关系:

GeUser.getFilters().get(0).getModel().getMake().getModels().get(0).getMake()...

我尝试在Model 类中添加@JsonIgnoreProperties(value = "models"),但仍然出现异常:

WARNING: Failed to write HTTP message: org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.gecars.objects.GeUser["filters"]->org.hibernate.collection.internal.PersistentSet[0]->com.gecars.objects.Filter["model"]->com.gecars.objects.models.Model_$$_jvste81_2["handler"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: com.gecars.objects.GeUser["filters"]->org.hibernate.collection.internal.PersistentSet[0]->com.gecars.objects.Filter["model"]->com.gecars.objects.models.Model_$$_jvste81_2["handler"])

实体 GeUser:

@Entity
@Table(name="users")
public class GeUser {
...
  @OneToMany(fetch = FetchType.LAZY)
  private Set<Filter> filters = new HashSet<Filter>(0);
..

过滤器:

@Entity
@Table(name="filters")
public class Filter {
...
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "fk_model") 
  private Model model;
..

型号:

@Entity
@Table(name="models")
public class Model {
...
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "fk_make") 
  @JsonIgnoreProperties(value = "models") //Not working (if I correct understand it should tell jackson to skip models from Make class )
  private Make make;
... 

制作:

@Entity
@Table(name="auto")
public class Make {
....
   @OneToMany(fetch = FetchType.LAZY, mappedBy = "make")
   @JsonIgnoreProperties(value = "make")
   private List<Model> models = new ArrayList<Model>(0);
...

【问题讨论】:

标签: java spring hibernate jackson


【解决方案1】:

要告诉杰克逊在将实体解析为 Json 时忽略属性,您可以使用 @JsonIgnore 注释:

@JsonIgnore
private PropertyClass property;

另一方面,所有实体类都必须实现java.io.Serializable

【讨论】:

  • @JsonIgnoreProperties(value = "models") 和@JsonIgnore 有什么区别?
  • 当您想从 JSON 序列化和反序列化过程中排除某些类成员时,Jackson 有两个不同的注释可供使用。这两个注解是@JsonIgnore@JsonIgnoreProperties@JsonIgnoreProperties 是类级别的注释,它期望要排除的属性将以字符串列表的形式显式指示。 @JsonIgnore 反而是成员级别或方法级别的注解,它期望被排除的属性被一一标记。
猜你喜欢
  • 2018-11-07
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
  • 2011-12-02
  • 2018-04-17
  • 2023-04-10
  • 2017-09-29
  • 2016-10-07
相关资源
最近更新 更多