我使用Java
Spring
web控制器和jackson
来解析响应。另外,我有四个Java
Hibernate
实体:User
Filter
Make
Model
,在制造商-〉型号中具有双向关系:
(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);
...
1条答案
按热度按时间unftdfkk1#
要告诉Jackson在将实体解析为Json时忽略某个属性,可以使用
@JsonIgnore
注解:另一方面,所有实体类都必须实现
java.io.Serializable