I need to create a POST endpoint that accepts a CSV file of DTOs.
How do I achieve it?
Spring Web can automatically convert JSONs to my custom objects, so I don't have to accept a string and then parse it myself. Can Spring Web do the same thing for CSVs? Or do I have to actually accept a file and then parse it manually myself (probably using some external library, like Apache Commons Csv)?
// what I want to work but what apparently doesn't @PostMapping(path = "/batch", consumes = "text/csv") public ResponseEntity<Void> postSocksBatch(List<SocksRequestDto> socksRequestDtos) { socksRequestDtos.forEach(socksService::save); return ResponseEntity.status(HttpStatus.CREATED).build(); }
Attempt 1
I got 415 Unsupported Media Type
@PostMapping(path = "/batch", consumes = "text/csv") public ResponseEntity<Void> postSocksBatch(@RequestBody Object file) {
curl --location 'http://localhost:8080/api/socks/batch' \--header 'Content-Type: text/csv' \--data-binary '@/C:/Users/nadch/OneDrive/Рабочийстол/socks.csv'
Attepmt 2
500 Internal Server Error
@PostMapping(path = "/batch", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ResponseEntity<Void> postSocksBatch(@RequestBody MultipartFile file) {
curl --location 'http://localhost:8080/api/socks/batch' \--header 'Content-Type: multipart/form-data' \--data-binary '@/C:/Users/nadch/OneDrive/Рабочийстол/socks.csv'
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
LettingContent-Type
blank resulted in 415