简体   繁体   English

Spring Boot可选的多部分POST请求

[英]Spring Boot optional multipart POST request

I have a service where I want to be able to optionally upload a file (including a file will run a separate function) with a POST request. 我有一项服务,希望通过POST请求有选择地上传文件(包括文件将运行单独的功能)。

A simplified version of what my ReqestMapping looks like is this: 我的ReqestMapping简化版本如下:

@ApiOperation(value = "Data", nickname = "Create a new data object")
@RequestMapping(value = "/add/{user_id}", produces = "application/json", method = RequestMethod.POST)
public ResponseEntity<Data> addData(@RequestParam("note") String body,
                                            @RequestParam("location") String location,
                                            @RequestParam(value = "file", required = false) List<MultipartFile> file,
                                            @PathVariable String user_id){
    if (file != null) {
        doSomething(file);
    }
    doRegularStuff(body, location, user_id);
    return new ResponseEntity(HttpStatus.OK);
}

As can be seen, I have the required = false option for my List of multipart files. 可以看出,我的多部分文件列表中有required = false选项。 However, when I attempt to curl the endpoint without any files and while stating that my content type is Content-Type: application/json , I get the error that my request isn't a multipart request. 但是,当我尝试在不包含任何文件的情况下curl端点并声明我的内容类型为Content-Type: application/json ,出现错误消息,我的请求不是多部分请求。

Fine. 精细。 So I change to Content-Type: multipart/form-data and without any files, I get the request was rejected because no multipart boundary was found (obviously, since I don't have a file). 因此,我更改为Content-Type: multipart/form-data并且没有任何文件,但the request was rejected because no multipart boundary was found (显然,因为我没有文件)。

This leads me to wonder how I can have a optional multipart parameter in my Spring endpoints? 这使我想知道如何在Spring端点中有一个可选的multipart参数? I would like to avoid having to add additional parameters to my request, such as "File Attached: True/False" as that can become cumbersome and unnecessary when the server can just check for existence. 我希望避免在我的请求中添加其他参数,例如“ File Attached:True / False”,因为当服务器仅检查存在性时,这将变得很麻烦且不必要。

Thanks! 谢谢!

There is no problem in your code, but the problem in client request, because Content-Type should be like below if you want to upload image, 您的代码没有问题,但是客户端请求中有问题,因为如果要上传图像,Content-Type应该如下所示,

multipart/form-data; boundary="123123"

try to remove the Content-Type header and test, i will put one example for server code and client request 尝试删除Content-Type标头并进行测试,我将为服务器代码和客户端请求放一个示例

Server code: 服务器代码:

@RequestMapping(method = RequestMethod.POST, value = "/users/profile")
    public ResponseEntity<?> handleFileUpload(@RequestParam("name") String name,
                                   @RequestParam(name="file", required=false) MultipartFile file) {

        log.info(" name : {}", name);
        if(file!=null)
        {   
            log.info("image : {}", file.getOriginalFilename());
            log.info("image content type : {}", file.getContentType());
        }
        return new ResponseEntity<String>("Uploaded",HttpStatus.OK);
    }

Client Request using Postman 使用邮递员的客户请求

with image 与图像 在此处输入图片说明

without image 没有图像

在此处输入图片说明

Curl example: 卷曲示例:

without image, with Content-Type 没有图像,具有内容类型

curl -X POST  -H "Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" -F "name=test" "http://localhost:8080/api/users/profile"

without image, without Content-Type 没有图像,没有内容类型

curl -X POST  -F "name=test" "http://localhost:8080/api/users/profile"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM