I want to send a form data object from angular "front" to spring boot "back" and I had the following error
Bad Request: Failed to convert value of type 'java.lang.String' to required type 'java.lang.Long'; nested exception is java.lang.NumberFormatException: For input string: "null"
create(document: IDocument, fichier: File | undefined): Observable<EntityResponseType> {
const formData: FormData = new FormData();
const documentDTO = new Blob([JSON.stringify({document})], {type:
formData.append('documentDTO', documentDTO);
if (fichier !== undefined) {
formData.append('fichier', fichier);
}
return this.http.post(this.resourceUrl, formData, { observe: 'response' });
}
@PostMapping(value = "/documents",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
public ResponseEntity<DocumentDTO> createDocument(@RequestPart("documentDTO") DocumentDTO documentDTO,
@RequestPart("fichier") MultipartFile fichier) throws URISyntaxException, SQLException {
log.debug("=========================", documentDTO.toString());
log.debug("+++++++++++++++++++++++++++++++++", fichier.getName());
this.documentService.init();
try {
Files.copy(fichier.getInputStream(), this.root.resolve(fichier.getOriginalFilename()));
} catch (Exception e) {
throw new RuntimeException("Could not store the file. Error: " + e.getMessage());
}
if (documentDTO.getId() != null) {
throw new BadRequestAlertException("A new document cannot already have an ID", ENTITY_NAME, "idexists");
}
DocumentDTO result = documentService.save(documentDTO);
return ResponseEntity
.created(new URI("/api/documents/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, true, ENTITY_NAME, result.getId().toString()))
.body(result);
}