Map multipart data to POJO on Spring WebFlux

Viewed 48

On the frontend we have a form which send the data as multipart/form-data and we need to receive the data in the controller. Keys nameconvention is to use hyphens.

We can receive the data in the controller by using @RequestPart annotation

@PostMapping(value = "/create", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Mono<String> create(@RequestPart("first-name") String firstName, 
                           @RequestPart("last-name") String lastName) {
// ...
}

But the form can contains more fields, and in this case it is not so convenient to receive data by using @RequestPart.

Is there a way to map multipart fields to a POJO on Spring WebFLux? Something like

class UserData {
    private String firstName;
    private String lastName;
    // getters & setters
}

@PostMapping(value = "/create", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Mono<String> create(UserData userData) {
// ...
}
0 Answers
Related