java发送文件和json Spring引导和角度
我想将一个表单数据对象从角度“front”发送到spring boot“back” 我犯了以下错误
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);
}
共 (0) 个答案