4

I use Java Spring web controller And jackson for parsing responses. Also I have four Java Hibernate entities: User Filter Make Model, with bidirectional relation in Make -> Model(s):

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

I tried add @JsonIgnoreProperties(value = "models") in Model class but still get exception:

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"])

Entities GeUser:

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

Filter:

@Entity
@Table(name="filters")
public class Filter {
...
  @ManyToOne(fetch = FetchType.LAZY)
  @JoinColumn(name = "fk_model") 
  private Model 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;
... 

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);
...
Edgaras Karka
  • 7,400
  • 15
  • 61
  • 115
  • Take a look at: [Jackson/Hibernate, meta get methods and serialization](https://stackoverflow.com/questions/55383889/jackson-hibernate-meta-get-methods-and-serialization). Probably, you need to use [Hibernate module](https://github.com/FasterXML/jackson-datatype-hibernate) – Michał Ziober Dec 02 '20 at 13:04

1 Answers1

0

to tell jackson to ignore a property while parsing an entity to Json you can use @JsonIgnore annotation:

@JsonIgnore
private PropertyClass property;

on the other hand, all Entities classes have to implements java.io.Serializable

Nebras
  • 636
  • 1
  • 7
  • 16
  • What is diference between @JsonIgnoreProperties(value = "models") and @JsonIgnore? – Edgaras Karka Feb 01 '16 at 11:09
  • **Jackson** has two different annotations to use when you want to exclude some class members from the JSON serialization and deserialization processes. These two annotations are `@JsonIgnore` and `@JsonIgnoreProperties`. `@JsonIgnoreProperties` is an annotation at the class level and it expects that the properties to be excluded would be explicitly indicated in the form of a list of strings. `@JsonIgnore` instead is a member-level or method-level annotation, which expects that the properties to be excluded are marked one by one. – Nebras Feb 01 '16 at 11:24