0

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);
    }
R. Richards
  • 24,603
  • 10
  • 64
  • 64
  • 1
    Do you have a Long in DocumentDTO? Can you post the structure of DocumentDTO and IDocument? – mep Aug 08 '21 at 16:01
  • Does this answer your question? [Spring Boot controller - Upload Multipart and JSON to DTO](https://stackoverflow.com/questions/49845355/spring-boot-controller-upload-multipart-and-json-to-dto) – eol Aug 08 '21 at 16:07
  • ` public class DocumentDTO implements Serializable { private Long id; @NotNull private String titre; @NotNull private String auteur; private String description; private String url; private TypeDocumentDTO typeDocument; private AnneeRedactionDocumentDTO anneeRedactionDocument; ` – markoye Aug 08 '21 at 16:14
  • ` import { ITypeDocument } from 'app/entities/type-document/type-document.model'; import { IAnneeRedactionDocument } from 'app/entities/annee-redaction-document/annee-redaction-document.model'; export interface IDocument { id?: number; titre?: string; auteur?: string; description?: string | null; url?: string | null; typeDocument?: ITypeDocument | null; anneeRedactionDocument?: IAnneeRedactionDocument | null; // fichier?: File | null; } ` – markoye Aug 08 '21 at 16:16

0 Answers0