Search code examples
angularrestspring-bootjacksonjackson2

Why all value null in request body?


I am calling a REST API from angular 4 client here is angular client code :

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

}

All values available in question object till this point. But when control goes in 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);
}

}

all question properties are becomes null please see screenshot :

sp

enter image description here

Here is my Question model 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 +
            '}';
}

}

Any help will appreciable, thanks in advance.


Solution

  • Your fields on frontend are called like _answers. You don't have this _prefix in your POJO field names. Those names must match if you want this to work out of the box. Also you canuse @JsonProperty to specify which JSON field maps to which POJO field - alternatively it is possible to setup parser in a way to automatically expect _ in front of every property.

    Next, you dont have to use JSON.stringify, just pase JS object. I am wondering if that does not changing Content-Type header.