我想做的是从数据库中检索所有 Questions 。 但是,相反,我在响应主体中遇到了无限循环。 我已经尝试过@JsonIgnore和@JsonManagedReference,就像其他类似主题所建议的那样,它对我不起作用... 我该如何解决?
课程:
@Entity
@Table(name = "Quest_table")
public class Quest {
@Id
@SequenceGenerator(name = "quest_seq", sequenceName = "quest_seq", initialValue = 1, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "quest_seq")
@Column(name = "quest_id")
private Integer id;
@Column(name = "Name", nullable = false)
private String name;
@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "Owner_id", nullable = false)
@JsonManagedReference
private User owner;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "Executor_id", nullable = false)
private User executor;
。
@Entity
@Table(name = "user_table")
public class User implements UserDetails {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_seq")
@SequenceGenerator(name = "user_seq", sequenceName = "user_seq", initialValue = 1, allocationSize = 1)
@Column(name = "user_id")
private Integer id;
@Column(name = "email",
unique = true)
@NotNull
@Email
private String email;
@ManyToOne(optional = false, fetch = FetchType.EAGER)
@JoinColumn(name = "role_id")
@JsonIgnore
private Role role;
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "password_id")
@JsonIgnore
private Password passwordHash;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "owner", targetEntity =
Quest.class)
@JsonBackReference
private Set<Quest> quests;
。
@Entity
@Table(name = "password_table")
public class Password implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "pass_seq")
@SequenceGenerator(name = "pass_seq", sequenceName = "pass_seq", initialValue = 1, allocationSize = 1)
@Column(name = "password_id")
@NotNull
private Integer id;
@Column(name = "hash",
nullable = false,
length = 100)
@NotNull
@Size(max = 100)
private String hash;
@OneToOne(cascade = CascadeType.ALL,
fetch = FetchType.LAZY,
mappedBy = "passwordHash")
private User user;
这是RestController:
@RestController
公共类QuestController {
@Autowired
private QuestRepository questRepository;
@PostMapping("/quests")
public ResponseEntity<Object> createQuest(@RequestBody Quest quest){
Quest savedQuest = questRepository.save(quest);
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(savedQuest.getId()).toUri();
return ResponseEntity.created(location).build();
}
响应示例:
{"id":1,"name":"Quest1","description":null,"photo":null,"points":10,"accepted":true,"validTill":null,"owner":{"id":3,"email":"weronika.sawicka@xyz.pl","firstName":"weronika","lastName":"sawicka","instagram":null,"facebook":null,"role":{"id":2,"roleName":"child"},"passwordHash":{"id":3,"hash":"$2a$10$/7Mqd9W5Vsm5dEXrV.g/TeR8YzJyK5fZ17XvHVB/tHzj450LtIWVi","user":{"id":3,"email":"weronika.sawicka@xyz.pl","firstName":"weronika","lastName":"sawicka","instagram":null,"facebook":null,"role":{"id":2,"roleName":"child"},"passwordHash":{"id":3,"hash":"$2a$10$/7Mqd9W5Vsm5dEXrV.g/TeR8YzJyK5fZ17XvHVB/tHzj450LtIWVi","user":{"id":3