gpt4 book ai didi

java - 我可以在 Spring Boot 中为函数添加 "get"前缀吗

转载 作者:太空宇宙 更新时间:2023-11-04 11:17:24 24 4
gpt4 key购买 nike

我有一个与 MongoRepository 关联的 Mcq 类,我想获取一个应用了多项更改的 Mcq 实例(答案随机播放、问题绘制等)。我声明了我的函数“myMcq.getInstance()”,但我不能这样做,因为每次我想在 ResponseEntity 中发送 Mcq 时,JSON 输出中都会出现错误,因为 Springboot 认为我的类中有一个“instance”属性。

这是我的 java 类:

@Document(collection = "Mcqs")
public class Mcq {
@Id public String id;
@DBRef public User creator;

public String title;
public String categoryID;
public List<McqChapter> chapterList = new ArrayList<>();
public Difficulty difficulty;

public Mcq() {}

public Mcq(String title) {
this();
this.title = title;
}

public ArrayList<String> getQuestionsIDs() {
ArrayList<String> result = new ArrayList<>();

for (McqChapter chapter : chapterList) result.addAll(chapter.getQuestionIDs());

return result;
}


public McqInstance getInstance() {
return new McqInstance(this);
}
}

最佳答案

为了防止错误添加 @JsonIgnoregetInstance() 方法:

@JsonIgnore 
public McqInstance getInstance() {
return new McqInstance(this);
}

Marker annotation that indicates that the annotated method or field is to be ignored by introspection-based serialization and deserialization functionality. That is, it should not be consider a "getter", "setter" or "creator".

关于java - 我可以在 Spring Boot 中为函数添加 "get"前缀吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45302337/

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