如何启用Spring Reactive Web MVC来处理多部分文件? [英] How to enable Spring Reactive Web MVC to handle Multipart-file?

查看:118
本文介绍了如何启用Spring Reactive Web MVC来处理多部分文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Spring Boot 2.0应用程序中使用新的反应式Web-mvc实现.我正在尝试定义一个消耗多部分文件的方法,但未能成功使它起作用:(-我总是会收到415错误.

I'm trying to use the new reactive web-mvc implementation in a spring boot 2.0 application. I'm trying to define a method which consume multipart file but do not succeed at making it working :( - I always get a 415 error.

一方面,我有一个包含以下请求映射的控制器:

On one hand I have a controller containing the following request mapping :

@RequestMapping(method = RequestMethod.POST, path = "/myPath/{param}/{param2}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@ResponseBody
public Mono<Void> postFile(
        @RequestBody MultipartFile data,
        @PathVariable("param") String param,
        @PathVariable("param2") String param2,
        @RequestHeader(name = HEADER_DATE, required = false)   @DateTimeFormat(pattern = DATE_FORMAT) Instant instant
){
    return fileService.handleData(Mono.just(data), param, param2, instant);
}

另一方面,我不得不在基本依赖项的顶部添加服务器,因为netty似乎无法处理多部分文件.因此,我添加了spring-boot-starter-tomcat依赖关系,该依赖关系使MultipartAutoConfiguration能够在应用程序自动配置上得到匹配和满足.

On the other hand I had to add a server on the top of the basic dependencies as it seems netty do not handle multipart files. I so added the spring-boot-starter-tomcatdependency which enabled the MultipartAutoConfiguration to be matched and satisfied on application auto configuration.

使用curl调用发布内容时: curl 'Meta-Date: 20170101104532' --form "file=@file.bin" http://localhost:8082/myPath/foo/bar 在激活调试日志(logging.level.org.springframework.web=DEBUG)时,出现了此异常: org.springframework.web.server.UnsupportedMediaTypeStatusException: Request failure [status: 415, reason: "Content type 'multipart/form-data;boundary=------------------------58fa43b8f1a26de4' not supported"]

When posting something using a curl call : curl 'Meta-Date: 20170101104532' --form "file=@file.bin" http://localhost:8082/myPath/foo/bar while debug logs are activated (logging.level.org.springframework.web=DEBUG) I got this exception : org.springframework.web.server.UnsupportedMediaTypeStatusException: Request failure [status: 415, reason: "Content type 'multipart/form-data;boundary=------------------------58fa43b8f1a26de4' not supported"]

具有以下受支持的媒体类型的RequestBodyArgumentResolver引发此错误:9 DecoderHttpMessageReader提供的[*/*, text/xml, application/*+json;charset=UTF-8, application/xml, text/plain;charset=UTF-8, application/x-www-form-urlencoded, application/json;charset=UTF-8].

This error is thrown by the RequestBodyArgumentResolver which has the the following supported media types : [*/*, text/xml, application/*+json;charset=UTF-8, application/xml, text/plain;charset=UTF-8, application/x-www-form-urlencoded, application/json;charset=UTF-8] provided by 9 DecoderHttpMessageReader.

发布之前,我还查看了:

Before posting I also took a look at :

  • Spring MultiPart MediaType Unsupported which seems to not be relevant here as my autoconf report contains the following entry : MultipartAutoConfiguration#multipartResolver matched
  • set content-type to utf-8 with angularjs $http Adding a header setting Content-Transfer-Encoding: binary didn't changed anything.

我的理解是Spring Web 5.0使用了新的请求解码器系统,因为我没有在spring 4 spring boot应用程序中找到这些类,并且还没有任何DecoderHttpMessageReader处理多部分文件 我错过了什么 ?还是应该等一个实施?

My understanding is that Spring web 5.0 uses a new request decoder system as I don't find these classes on a spring 4 spring boot application, and there is not yet any DecoderHttpMessageReader dealing with multipart file Did I miss something ? Or should I wait one to be implemented ?

推荐答案

好吧,似乎暂时还没有实现,因为它当前存在对此功能的拉取请求:

Okay, It seems this is just not implemented for now as it currently exists a pull request for this feature : Add reactive multipart request support #1201

应该早点检查一下...

Should have check this earlier...

:该问题已解决,已合并到Spring master分支中.应该不再是一个问题.

: The issue has been solved and merged into Spring master branch. Should no longer be an issue.

这篇关于如何启用Spring Reactive Web MVC来处理多部分文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆