简体   繁体   中英

jackson spring field missing from json serialization

The Action field is missing from the json serialization on this Question class. I know there are actions for the question I'm querying because I looked at the question object before it was returned in the 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;

}

Here is the Action class

@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;
}

I have an ActionController and that returns a json string for the class. Why would the Action class be missing from the json for Question?

Here is the 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;
    }
}

Here is the question controller. When I put a break point in it I can see the actions in the question objects, but the json returned doesn't have a field for them

@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;
    }

}

It's because You are using

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

Please refer to this 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.

在父表实体中需要指定一个joinColumns=@JoinColumn(name="ID")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM