I have a reactive web application and looking for file upload with input data.
Working code for File upload,
Router,
.route(POST("/api/v1/upload").and(contentType(MULTIPART_FORM_DATA)),
patientRequestHandler::uploadFile)
Handler,
public Mono<ServerResponse> uploadFile(ServerRequest request) {
final String id = request.pathVariable("id");
return request.body(BodyExtractors.toMultipartData()).flatMap(parts -> {
Map<String, Part> partMap = parts.toSingleValueMap();
Working code for input data,
Router,
.andRoute(POST("/api/v1/profile").and(contentType(APPLICATION_JSON)),
requestHandler::createProfile)
Handler,
public Mono<ServerResponse> createProfile(ServerRequest request) {
return request.bodyToMono(ProfileDto.class).flatMap(patient -> {
@JsonInclude(Include.NON_NULL)
public class ProfileDto {
private Long id;
private String name;
Now, I would like to have form data submission along with the file upload.
Not sure, How to do that?
I tried the below,
Route,
.route(PUT("/api/v1/upload").and(accept(MULTIPART_FORM_DATA, APPLICATION_JSON)), branchHandler::updateSetting);
DTO,
@JsonInclude(Include.NON_NULL)
public class ImageWrapper extends ProfileDto {
private FilePart image;
public FilePart getImage() {
return image;
}
public void setImage(FilePart image) {
this.image = image;
}
}
Handler,
public Mono<ServerResponse> updateSetting(ServerRequest request) {
final LocalDateTime updatedTime = LocalDateTime.now();
request.bodyToMono(ImageWrapper.class).flatMap(profileDto -> {
return null;
});
}
Here,
I tried with postman.
Postman Error,
{
"timestamp": "2021-06-15T19:07:52.763+00:00",
"path": "/api/v1/service/profile",
"status": 415,
"error": "Unsupported Media Type",
"requestId": "ce7a2dc9-1"
}
