I am trying to POST to http://localhost:9095/translators from an AngularJS front-end using Spring boot/Spring RestController backend.
I can do a GET and the response
Another great way to solve this problem is with @JsonView. The idea is that you label your controller with a view name and then label the properties you wish to be displayed for that view. You specifically do not expose the backreferenced properties to the calling view. An extremely simplified example below:
Imagine you had a 1 to 1 relationship like this. This would create a circular reference.
@Entity
public class Student {
String name;
Tutor tutor;
}
@Entity
public class Tutor {
String name;
Student student;
}
You could now create views for them, almost the same way you do with @JsonIgnore and @JsonProperty.
public class LearningController {
@GetRequest("/tutors")
@JsonView(TutorView.class) // create an empty interface with a useful name
public Set tutors() {
return tutorRepository.findAll()
}
@GetRequest("/students")
@JsonView(StudentView.class) // create an empty interface with a useful name
public Set students() {
return studentRepository.findAll()
}
}
@JsonView annotation.@Entity
public class Student {
@JsonView({StudentView.class, TutorView.class})
String name;
@JsonView({StudentView.class}) // Not visible to @TutorView (no backreference)
Tutor tutor;
}
@Entity
public class Tutor {
@JsonView({StudentView.class, TutorView.class})
String name;
@JsonView(TutorView.class) // Not visible to @StudentView (no backreference)
Student student;
}