2

I have a working solution but it seems silly that it's needed.

This is my working solution:

@PreAuthorize("isAuthenticated()")
@ApiOperation(value = "Takes in a document.", nickname = "Document Upload", response = DocumentResponse[].class)
@ResponseStatus(HttpStatus.ACCEPTED)
@RequestMapping(
        value = "/api/v1/document/upload",
        produces = "application/json",
        consumes = "multipart/form-data",
        method = RequestMethod.POST)
public DocumentResponse uploadDocument(
        // THIS is where I am using a String and don't want to.
        @RequestPart("fileData") String fileData, 
        @RequestPart("file") MultipartFile file,
        @RequestHeader("idempotency-id") String idempotencyId) throws IOException {

    // THIS is the line I would also like to avoid.
    DocumentUploadFileData fileDataObj = objectMapper.readValue(fileData, DocumentUploadFileData.class);

    printBeanValues(fileDataObj);

    More after....

The trouble is that fileData is a String object. I would like to have spring map the JSON directly to my DocumentUploadFileData class without having to do it myself as seen here: DocumentUploadFileData fileDataObj = objectMapper.readValue(fileData, DocumentUploadFileData.class);

What I have tried:

  1. Instead of @RequestPart I just used nothing. I actually thought that would be fine. It wasn't.
  2. List item @RequestBody. I thought for sure this would work. Instead, it just started yelling at me about my content/type not being valid? The content type didn't change but for some reason it wanted it to be application/json (even though I'm saying multipart/form-data explicitly. I guess @ReqestBody is intended for application/json requests and it doesn't like playing with multipart?
  3. I also tried using RequestParam, that actually works if I use a String as the object. If I try to use my DocumentUploadFileData instead it fails and tells me it doesn't have a mapping strategy for the object? I think something about the fact that it's a multipart request makes spring decide to use different mappers that maybe I need to add? I know multipart requests use boundaries and are just a little different generally so it makes sense that it might want a different solution. I just don't know how to provide that.

I haven't used Spring for 3 years I'm sure the solution isn't that complicated, however, I still haven't gotten it after several hours.

njfife
  • 3,555
  • 1
  • 20
  • 31

1 Answers1

0

Try to adept the following solution noted here: Spring MVC Multipart Request with JSON

@RequestMapping(value = "/executesampleservice", method = RequestMethod.POST,
        consumes = {"multipart/form-data"})
    @ResponseBody
    public boolean executeSampleService(
            @RequestPart("properties") @Valid ConnectionProperties properties,
            @RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
        return projectService.executeSampleService(properties, file);
    }
Simon
  • 925
  • 3
  • 13
  • 30