1

How should I map form data request with multiple files and string params?

The request looks below, user can upload multiple files and each file belongs to user(array of user data in data param, in data there is file param in each object)

enter image description here

Here data should be mapped in the list of User class

public class User {
    private String email;
    private String fileName;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
}

and request mapping should like

@PostMapping(path = ExpertRestURI.EXPERT_SKILL, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity saveData(@ModelAttribute List<MultipartFile> files, @Valid List<User> users)

This is not working, any alternative solutions/suggestions for this kind of scenario?

Nitin
  • 1,882
  • 1
  • 14
  • 42

1 Answers1

0
public ResponseEntity saveData(@RequestPart(value= "file") MultipartFile[] files, @Valid @RequestPart(value = "data") List<User> users)

You should call the postman key as file and change your data value as

[
    { "email": "email",
      "fileName" "file1"
    },

    { "email": "email2",
      "fileName" "file12"
    }
 ]
Hatice
  • 693
  • 1
  • 7
  • 16
  • Not working, it throws exception `org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/octet-stream' not supported` Ref: https://stackoverflow.com/questions/50395010/spring-postman-content-type-application-octet-stream-not-supported – Nitin Jan 25 '20 at 04:57