Spring webFlux infinite recursion
Spring webFlux infinite recursion
So I'am trying Spring webFlux and have following code:
@Override
public Flux<? extends AnimalDatabaseEntity> queryAnimals() {
return async(animalsRepository.findAll().stream());
}
private <T> Flux<T> async(Stream<T> stream) {
return Flux.fromStream(stream).publishOn(scheduler);
}
The problem that I get infinite recursion, because "AnimalDatabaseEntity" has field animalFeatures.
@Entity(name = "animals")
public class AnimalDatabaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Enumerated(EnumType.STRING)
@Column(name = "animal_type")
private AnimalType animalType;
@Column(name = "number_of_legs")
private Integer numberOfLegs;
@Column(name = "is_pet")
private boolean isPet;
@OneToMany(mappedBy = "animalDatabaseEntity", fetch = FetchType.EAGER)
private List<AnimalFeatureEntity> animalFeatures;
}
I don't really get why this does not work???
1 Answer
1
My best guess is AnimalFeatureEntity
contains varaible of type AnimalDatabaseEntity
which references back to List<AnimalFeatureEntity>
while serializing thus causing an infinite recursion.
You can prevent it by adding @JsonIgnore
to AnimalDatabaseEntity
if it suits your use case.
However, if you wish to preserve the bidirectional relationship, then @JsonManagedReference/@JsonBackReference
or @JsonIdentityInfo
is the way to go.
For example usage, refer this article
AnimalFeatureEntity
AnimalDatabaseEntity
List<AnimalFeatureEntity>
@JsonIgnore
AnimalDatabaseEntity
@JsonManagedReference/@JsonBackReference
@JsonIdentityInfo
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Comments
Post a Comment