英文:
Spirng @Post method receives data as NULL
问题
I have an Web-service to add/edit/update/delete Users and their Notes. When I use @Post method in postman to add a note, it adds its body as NULL. After these values added into database, I use @Put method to update them with the same Body parameters
, and they are accepted as they should be.
My DTO for Notes looks like this.
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NoteDto {
private int noteId;
private String topic;
private String noteBody;
}
My NotesServiceImpl
looks like this:
@Override
public NoteDto createNote(int userId, NoteDto noteDto) {
Note review = mapToEntity(noteDto);
User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException("User with associated id not found"));
review.setUser(user);
Note newReview = notesRepository.save(review);
return mapToDto(newReview);
}
My Controller for @Post
method looks like this:
@PostMapping("/user/{userId}/notes")
public ResponseEntity<NoteDto> createNote(@PathVariable(value = "userId") int userId, @RequestBody NoteDto noteDto) {
return new ResponseEntity<>(noteService.createNote(userId, noteDto), HttpStatus.CREATED);
}
This is how @Post
output looks like:
英文:
I have an Web-service to add/edit/update/delete Users and their Notes. When I use @Post method in postman to add a note, it adds its body as NULL. After these values added into database, I use @Put method to update them with the same Body parameters
, and they are accepted as they should be.
My DTO for Notes looks like this.
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class NoteDto {
private int noteId;
private String topic;
private String noteBody;
}
My NotesServiceImpl
looks like this:
@Override
public NoteDto createNote(int userId, NoteDto noteDto) {
Note review = mapToEntity(noteDto);
User user = userRepository.findById(userId).orElseThrow(() -> new UserNotFoundException("User with associated id not found"));
review.setUser(user);
Note newReview = notesRepository.save(review);
return mapToDto(newReview);
}
My Controller for @Post
method looks like this:
@PostMapping("/user/{userId}/notes")
public ResponseEntity<NoteDto> createNote(@PathVariable(value = "userId") int userId, @RequestBody NoteDto noteDto) {
return new ResponseEntity<>(noteService.createNote(userId, noteDto), HttpStatus.CREATED);
}
答案1
得分: 1
I fixed it. The problem was with my MapToEntity
code. I didn't map Dto properly.
Before it was:
private Note mapToEntity(NoteDto noteDto) {
Note note = new Note();
note.setId(note.getNoteId());
note.setTopic(note.getTopic());
note.setNoteBody(note.getNoteBody());
return note;
}
I changed it to:
private Note mapToEntity(NoteDto noteDto) {
Note note = new Note();
note.setId(noteDto.getNoteId());
note.setTopic(noteDto.getTopic());
note.setNoteBody(noteDto.getNoteBody());
return note;
}
and that solved it.
英文:
Okay, I fixed it. The problem was with my MapToEntity
code. I didn't map Dto properly.
Before it was:
`private Note mapToEntity(NoteDto noteDto) {
Note note = new Note();
note.setId(note.getNoteId());
note.setTopic(note.getTopic());
note.setNoteBody(note.getNoteBody());
return note;
}`
I changed it to:
private Note mapToEntity(NoteDto noteDto) {
Note note = new Note();
note.setId(noteDto.getNoteId());
note.setTopic(noteDto.getTopic());
note.setNoteBody(noteDto.getNoteBody());
return note;
}
and that solved it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论