gpt4 book ai didi

java - json 序列化中缺少 Jackson Spring 字段

转载 作者:行者123 更新时间:2023-12-02 02:49:57 24 4
gpt4 key购买 nike

此 Question 类的 json 序列化中缺少 Action 字段。我知道我正在查询的问题有一些操作,因为我在 Controller 返回问题对象之前查看了问题对象

@Entity
@Table
@Data
public class Question {

@Id
@GeneratedValue
@Column
private int id;

@Column
private String text;

@Column
private double weight;

@Enumerated(EnumType.STRING)
private TeamType teamType;

@Column
private Category cat;

@ManyToOne
@JsonBackReference(value="act-ques")
private Action action;

@OneToMany
@JsonManagedReference(value="ques-resps")
private Set<Response> response;

}

这是 Action 类

@Entity
@Table
@Data
public class Action {

@Id
@Column
@GeneratedValue
private Long id;

@Enumerated(EnumType.STRING)
private ActionType actionType;

@Enumerated(EnumType.STRING)
private Category cat;

@OneToMany
@Fetch(FetchMode.JOIN)
@JsonManagedReference(value="act-ques")
private Set<Question> questions;
}

我有一个 ActionController,它返回该类的 json 字符串。为什么问题的 json 中缺少 Action 类?

这是 ActionController

@RestController
@RequestMapping("api/actions")
public class ActionController {

@Autowired
private ActionService actService;

@GetMapping
public List<Action> getActions(@RequestParam Map<String, Object> params) {
List<Action> actions = actService.getAll();
return actions;
}
}

这是问题 Controller 。当我在其中放置断点时,我可以看到问题对象中的操作,但返回的 json 没有它们的字段

@RestController
@RequestMapping("api/questions")
public class QuestionController {

@Autowired
private QuestionService questionService;

@GetMapping
public List<Question> getQuestions(@RequestParam Map<String, Object> params) {
List<Question> questions = questionService.getQuestions(params);
return questions;
}

}

最佳答案

这是因为你正在使用

@JsonBackReference(value="act-ques")
private Action action;

请引用此blog

@JsonManagedReference is the forward part of reference – the one that gets serialized normally.
@JsonBackReference is the back part of reference – it will be omitted from serialization.

关于java - json 序列化中缺少 Jackson Spring 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44009811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com