在Jackson序列化过程中出现异常/JsonMappingException/JsonMapping.LAZY

jhiyze9q  于 2022-11-08  发布在  其他
关注(0)|答案(1)|浏览(144)

我使用JavaSpring web控制器和jackson来解析响应。另外,我有四个JavaHibernate实体:UserFilterMakeModel,在制造商-〉型号中具有双向关系:
(0)...................
我尝试在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);
...
unftdfkk

unftdfkk1#

要告诉Jackson在将实体解析为Json时忽略某个属性,可以使用@JsonIgnore注解:

@JsonIgnore
private PropertyClass property;

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

相关问题