为什么请求正文中的所有值都为空?

Why all value null in request body?

我正在从 angular 4 客户端调用 REST API 这里是 angular 客户端代码:

createQuestion(question: Question): Observable<Question> {
return this.http.post<Question>(API_URL + 'create/question', JSON.stringify(question),
  {headers: this.headers});

}

到目前为止,问题对象中的所有可用值。但是当控制进入 RestController 时:

@RestController
 @RequestMapping(AppUtilities.ROOT_MAPPING)
 public class QuestionController {

@Autowired
private QuestionService questionService;
@Autowired
private QuizService quizService;

@PostMapping("/create/question")
public Question save(@RequestBody Question question) {

    Quiz quiz = quizService.find(question.getQuiz().getId());
    question.setQuiz(quiz);

    return questionService.save(question);
}

}

所有问题属性都变为空请看截图:

这是我的问题模型 class :

  @Entity
@Table(name = "question")
public class Question extends BaseModel implements UserOwned {

@Size(min = 2, max = 150, message = "The question should be between 2 and 150 characters")
@NotNull(message = "Question text not provided")
private String text;

@ManyToOne
private Quiz quiz;

@Column(name = "a_order")
private Integer order;

@OneToMany(mappedBy = "question", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<Answer> answers;

@OneToOne(cascade = CascadeType.ALL)
private Answer correctAnswer;

@Column(columnDefinition = "TIMESTAMP DEFAULT CURRENT_TIMESTAMP", insertable = false, updatable = false)
private Calendar createdDate;

@JsonIgnore
private boolean isActive;

public String getText() {
    return text;
}

public void setText(String text) {
    this.text = text;
}

public Quiz getQuiz() {
    return quiz;
}

public void setQuiz(Quiz quiz) {
    this.quiz = quiz;
}

public Integer getOrder() {
    return order;
}

public void setOrder(Integer order) {
    this.order = order;
}

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

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

public Calendar getCreatedDate() {
    return createdDate;
}

public void setCreatedDate(Calendar createdDate) {
    this.createdDate = createdDate;
}

public boolean isActive() {
    return isActive;
}

public void setActive(boolean active) {
    isActive = active;
}

@Override
public User getUser() {
    return quiz.getUser();
}

public Answer getCorrectAnswer() {
    return correctAnswer;
}

public void setCorrectAnswer(Answer correctAnswer) {
    this.correctAnswer = correctAnswer;
}

@Override
public String toString() {
    return "Question{" +
            "text='" + text + '\'' +
            ", quiz=" + quiz +
            ", oreder=" + order +
            ", answers=" + answers +
            ", correctAnswer=" +  correctAnswer +
            ", createdDate=" + createdDate +
            ", isActive=" + isActive +
            '}';
}

}

任何帮助都将不胜感激,在此先感谢。

您在前端的字段名称类似于 _answers。您的 POJO 字段名称中没有此 _ 前缀。如果您希望开箱即用,这些名称必须匹配。您还可以使用 @JsonProperty 指定哪个 JSON 字段映射到哪个 POJO 字段 - 或者可以设置解析器以自动在每个 属性 前面期望 _ .

接下来不用JSON.stringify,直接pase JSobject即可。我想知道这是否不会改变 Content-Type header。

您的 JSON 的变量名称与您的 POJO

不匹配

假设你的 POJO 像

public class LoginDTO 
{
     private String username;
     private String password;
    //getters and setters
}                                                                                                                                                                                         

data = {'username':'xyz','password':'1233'} 

所以这里的问题是 _ 在您的 JSON 变量名称之前 您需要将名称与您的 POJO class

匹配

ref-:https://www.baeldung.com/spring-request-response-body