Spring @Post方法接收到的数据为NULL。

huangapple go评论77阅读模式
英文:

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: Spring @Post方法接收到的数据为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(() -&gt; new UserNotFoundException(&quot;User with associated id not found&quot;));

    review.setUser(user);

    Note newReview = notesRepository.save(review);

    return mapToDto(newReview);
}

My Controller for @Post method looks like this:

@PostMapping(&quot;/user/{userId}/notes&quot;)
public ResponseEntity&lt;NoteDto&gt; createNote(@PathVariable(value = &quot;userId&quot;) int userId, @RequestBody NoteDto noteDto) {
    return new ResponseEntity&lt;&gt;(noteService.createNote(userId, noteDto), HttpStatus.CREATED);
}

This is how @Post output looks like:Spring @Post方法接收到的数据为NULL。

答案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.

huangapple
  • 本文由 发表于 2023年4月20日 02:50:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057927.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定