Spring Web Reactive Framework Multipart File Issue

不羁的心 提交于 2019-12-04 06:54:35

After digging around I was able to find this test in the Spring WebFlux project:

https://github.com/spring-projects/spring-framework/blob/master/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MultipartIntegrationTests.java

So the part I was missing was @RequestPart instead of @RequestBody in the controller definition.

Final code looks something like this:

@RestController
@RequestMapping("/images")
public class ImageController {

    @Autowired
    private IImageService imageService;

    @PostMapping(value = "", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    Mono<ImageEntity> saveImage(@RequestPart("file") Mono<FilePart> part) throws Exception{
         return part.flatMap(file -> imageService.saveImage(file));
    }
}

Actually the following solution seems to work with Netty

    @PostMapping(path = "/test/{path}",
            consumes = MediaType.MULTIPART_FORM_DATA_VALUE, 
            produces = {MediaType.APPLICATION_JSON_VALUE})
    @ResponseBody
    Mono<String> commandMultipart(
        @PathVariable("path") String path,
        @RequestPart("jsonDto") Mono<JsonDto> jsonDto,
        @RequestPart(value = "file",required = false) Mono<FilePart> file) {
        JsonDto dto = jsonDto.block();
     }

Build.gradle

 compile group: 'org.synchronoss.cloud', name: 'nio-multipart-parser', version: '1.1.0'

 compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.3'
 compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.9.3'

curl command in bash

echo '{"test":"1"}' > command.json && curl -H "Content-Type:multipart/form-data" -X POST http://localhost:8082/test/examplepath/ -F "command=@./command.json;type=application/json" -F "file=@test.bin" -vv

Troubleshooting steps

  1. Ensure nio-multipart-parser is present by checking method org.springframework.http.codec.support.ServerDefaultCodecsImpl#extendTypedReaders

  2. You can check that nio-multipart-parser is used by placing breakpoint inside

    • org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader#canRead() for single part
    • org.springframework.http.codec.multipart.MultipartHttpMessageReader#canRead for multipart

    One of the above methods should return true.

In some cases the solution is to update the Spring version greater than 2.1.1, after this you should check that in the dependencies that are not 'spring-webmvc' since this generates a conflict with 'spring-boot-starter-webflux'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!