gpt4 book ai didi

java - 响应为空

转载 作者:行者123 更新时间:2023-12-01 21:08:30 25 4
gpt4 key购买 nike

当我这样做的时候

GET /survey/1

尽管我的数据库中有问题和答案,但我收到了此回复:

{
"surveyId": 1,
"name": "Example",
"questions": null,
"answers": null
}

为什么我的“问题”和“答案”为空?我该如何解决它?

调查存储库:

public interface SurveyRepository extends CrudRepository<Survey, Integer> { }

调查的模型类:

@Entity
@Table(name = "survey")
public class Survey {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "surveyId")
private Integer surveyId;

@Column(name = "name", nullable = false)
private String name;

@Transient
private List<String> questions;

@Transient
private List<String> answers;

public Survey() { }

public Survey(Integer surveyId, String name) {
this.surveyId = surveyId;
this.name = name;
}

public Integer getSurveyId() {
return surveyId;
}

public String getName() {
return name;
}

public List<String> getQuestions() {
return questions;
}

public List<String> getAnswers() {
return answers;
}

public void setSurveyId(Integer surveyId) {
this.surveyId = surveyId;
}

public void setName(String name) {
this.name = name;
}

public void setQuestions(List<String> questions) {
this.questions = questions;
}

public void setAnswers(List<String> answers) {
this.answers = answers;
}
}

调查的 Controller 类:

@RestController
@RequestMapping("/survey")
public class SurveyController {

@Autowired
private SurveyRepository surveyRepo;

@Autowired
private AnswerRepository answerRepo;

@Autowired
private QuestionRepository questionRepo;

@RequestMapping(method = RequestMethod.GET, value = "/{id}")
public Survey getSurveyById(@PathVariable("id") int id) {
return surveyRepo.findOne(id);
}

@RequestMapping(method = RequestMethod.POST)
public String create(@RequestBody Survey survey) {
surveyRepo.save(survey);
return "Survey created";
}

@RequestMapping(method = RequestMethod.GET)
public Iterable<Survey> getAllSurveys() {
return surveyRepo.findAll();
}

@RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
public String delete(@PathVariable("id") int id) {
surveyRepo.delete(id);
return "Survey deleted";
}

@RequestMapping(method = RequestMethod.PUT, value = "/{id}")
public String update(@PathVariable("id") int id, @RequestBody Survey survey) {
Survey update = surveyRepo.findOne(id);

update.setName(survey.getName());
update.setQuestions(survey.getQuestions());

surveyRepo.save(update);
return "Survey updated";
}

}

回答模型类:

@Entity
@Table(name = "answer")
public class Answer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "answerId")
private Integer answerId;

@Column(name = "answer", nullable = false)
private String answer;

@ManyToOne
@JoinColumn(name = "questionId", nullable = false)
private Question questionId;

public Answer() { }

public Answer(Integer answerId, String answer, Question questionId) {
this.answerId = answerId;
this.answer = answer;
this.questionId = questionId;
}

public Integer getAnswerId() {
return answerId;
}

public String getAnswer() {
return answer;
}

public Question getQuestionId() {
return questionId;
}

public void setAnswerId(Integer answerId) {
this.answerId = answerId;
}

public void setAnswer(String answer) {
this.answer = answer;
}

public void setQuestionId(Question questionId) {
this.questionId = questionId;
}

}

问题模型类

@Entity
@Table(name = "question")
public class Question {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "questionId")
private Integer questionId;

@Column(name = "question", nullable = false)
private String question;

@ManyToOne
@JoinColumn(name = "surveyId", nullable = false)
private Survey surveyId;

@Transient
private List<String> answers;

public Question() { }

public Question(Integer questionId, String question, Survey surveyId) {
this.questionId = questionId;
this.question = question;
this.surveyId = surveyId;
}

public Integer getQuestionId() {
return questionId;
}

public String getQuestion() {
return question;
}

public Survey getSurveyId() {
return surveyId;
}

public void setQuestionId(Integer questionId) {
this.questionId = questionId;
}

public void setQuestion(String question) {
this.question = question;
}

public void setSurveyId(Survey surveyId) {
this.surveyId = surveyId;
}
}

最佳答案

您将它们标记为@Transient,这取决于您使用的是哪一个,这意味着它不会被序列化,或者不会存储在数据库中。请参阅this answer.另外,不相关的您可能应该在这些 Lists

上使用 @ElementCollection(targetClass = String.class)

关于java - 响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41751352/

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