java 使用 Springfox 和 Swagger-ui 上传分段文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31420146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:35:46  来源:igfitidea点击:

Multipart File upload using Springfox and Swagger-ui

javaspringspring-mvcfile-uploadswagger-ui

提问by drumlord

I'm using Spring MVC as a rest controller and I've integrated Swagger-ui with my controller using Springfox. I'd like to have a method that is able to upload a file via the Swagger-ui interface. I only need two parameters, a long acting for an object id and the file to be uploaded.

我使用 Spring MVC 作为休息控制器,并且我使用 Springfox 将 Swagger-ui 与我的控制器集成在一起。我想要一种能够通过 Swagger-ui 界面上传文件的方法。我只需要两个参数,一个对象 id 的长效参数和要上传的文件。

@RestController
public class controller{
    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public void uploadFile(@RequestParam Long id,
                           @RequestParam MultipartFile file){
          //do some stuff
    }
}

I've tried almost everything and I can't get a file upload button to appear. However, if I do:

我几乎尝试了所有方法,但无法显示文件上传按钮。但是,如果我这样做:

@RestController
public class Controller{
    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public void uploadFile(@RequestParam Long id,
                           @RequestPart File file){
         //do some stuff
    }
}

The file upload button appears, but it always throws http code 415 when trying to upload a file. Besides, I need the input to be a MultipartFile, not a regular File. Even if I use the @RequestPart annotation with Multipart File, the choose file to upload button does not appear. How can I get this to work???? Even:

出现文件上传按钮,但在尝试上传文件时它总是抛出 http 代码 415。此外,我需要输入是 MultipartFile,而不是常规文件。即使我将 @RequestPart 注释与 Multipart File 一起使用,选择要上传的文件按钮也不会出现。我怎样才能让它工作????甚至:

@RestController
public class Controller{
    @RequestMapping(value="/upload", method=RequestMethod.POST)
    public void uploadFile(@RequestPart String metaData,
                           @RequestPart MultipartFile file){
        //do some stuff
    }
}

Won't work. If someone could give a walkthrough of how to get this button to appear for MultipartFile? I'd be forever grateful.

不会工作。如果有人可以演练如何让这个按钮出现在 MultipartFile 中?我会永远感激。

回答by ashario

In my situation, there were two things I needed to do

在我的情况下,我需要做两件事

  1. My MultipartFile request param had to be named 'file', otherwise, swagger-ui wouldn't display the file upload input control
  1. 我的 MultipartFile 请求参数必须命名为“文件”,否则 swagger-ui 不会显示文件上传输入控件
@RequestParam("file") MultipartFile file
@RequestParam("file") MultipartFile file
  1. I had to register the following bean
  1. 我必须注册以下bean
@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(){
    return new CommonsMultipartResolver();
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver commonsMultipartResolver(){
    return new CommonsMultipartResolver();
}

回答by Benn Sandoval

Use

利用

@RequestPart(required = true) MultipartFile file

@RequestPart(required = true) MultipartFile file

And use the version number 2.1.0 or latest, there is a bug with previous versions.

并使用2.1.0或最新版本号,以前的版本有bug。

https://github.com/springfox/springfox/issues/786

https://github.com/springfox/springfox/issues/786

回答by abedurftig

I think you are missing the consumesattribute of the @RequestMappingin your second snippet. See the following example

我认为您在第二个代码段中缺少@RequestMapping消耗属性。看下面的例子

@RequestMapping(
    path = "/upload", 
    method = RequestMethod.POST, 
    consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<String> handleUpload(
        @RequestPart("file") MultipartFile file, 
        @RequestParam("someId") Long someId,         
        @RequestParam("someOtherId") Long someOtherId) { 
    return new ResponseEntity<>();
}

回答by sudarsan - 406

Two things...

两件事情...

  1. Value of consumes should should be "multipart/form-data". consumes="multipart/form-data"

  2. @RequestPart("file") @ApiParam(value="File", required=true) MultipartFile file

  1. 消费的价值应该是“multipart/form-data”。消费=“多部分/表单数据”

  2. @RequestPart("file") @ApiParam(value="File", required=true) MultipartFile 文件