Spring says no data found in @RequestParam I tried changing @RequestPart or value but it doesn’t work. How can I make a connection? Thanks for any advice, solution
react
boardWriter: sessionStorage.getItem("memberID"),
boardTitle: title,
boardContents: contents,
boardFile: file,
}
formData.append("saveData", new Blob([saveData]), {
type: "application/json",
})
formData.append("boardFile", file);
axios
.post("http://localhost:8080/api/board/write", saveData, {
headers: {
"Content-Type": "multipart/form-data",
}
})
.then(res => res.data)
.catch(err => console.log(err));
}
controller
@PostMapping("/write")
public void save(@RequestPart("saveData") BoardDTO boardDTO,
@RequestPart(value = "boardFile") MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
file.transferTo(new File(fileName));
boardDTO.setBoardFile(fileName);
boardService.save(boardDTO);
}
repository
public int save(BoardDTO boardDTO) {
return sql.insert("Board.save", boardDTO);
}
service
public int save(BoardDTO boardDTO) {
return boardRepository.save(boardDTO);
}
**error**
400 http bad request
Required BoardDTO parameter 'boardDTO' is not present
2
Answers
You can use @RequestParam and @ModelAttribute annotations for DTO with file.
There is some other code because I reuse my code before wrote.
Focus how to get data at API endpoints.
Here is my example.
React
Spring
Better Way
I think this is the better way for management back-end code.
React
Spring
And you need to setter method for DTO.
Change this:
to this:
Also, it seems that you have mistakenly sent saveData instead of formData in the following code.
Please change it to:
This should make everything work.