Akzeptieren Sie CSV von DTOs im Spring Web REST-ControllerJava

Java-Forum
Guest
 Akzeptieren Sie CSV von DTOs im Spring Web REST-Controller

Post by Guest »

Ich muss einen POST-Endpunkt erstellen, der eine CSV-Datei mit DTOs akzeptiert.
Wie erreiche ich das?
Spring Web kann das automatisch Konvertieren Sie JSONs in meine benutzerdefinierten Objekte, sodass ich keinen String akzeptieren und ihn dann selbst analysieren muss. Kann Spring Web dasselbe für CSVs tun? Oder muss ich eine Datei tatsächlich akzeptieren und sie dann selbst manuell analysieren (wahrscheinlich mithilfe einer externen Bibliothek wie Apache Commons Csv)?

Code: Select all

// what I want to work but what apparently doesn't
@PostMapping(path = "/batch", consumes = "text/csv")
public ResponseEntity postSocksBatch(List socksRequestDtos) {
socksRequestDtos.forEach(socksService::save);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
Versuch 1
Ich habe 415 Nicht unterstützter Medientyp erhalten

Code: Select all

    @PostMapping(path = "/batch", consumes = "text/csv")
public ResponseEntity postSocksBatch(@RequestBody Object file) {

Code: Select all

curl --location 'http://localhost:8080/api/socks/batch' \
--header 'Content-Type: text/csv' \
--data-binary '@/C:/Users/nadch/OneDrive/Рабочий стол/socks.csv'
Image
Image
Versuch 2
500 Interner Serverfehler

Code: Select all

    @PostMapping(path = "/batch", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity postSocksBatch(@RequestBody MultipartFile file) {

Code: Select all

curl --location 'http://localhost:8080/api/socks/batch' \
--header 'Content-Type: multipart/form-data' \
--data-binary '@/C:/Users/nadch/OneDrive/Рабочий стол/socks.csv'
Image

Code: Select all

org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
Das Leerlassen von Content-Type führte zu 415

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post