Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
6.5k views
in Technique[技术] by (71.8m points)

spring boot - Sending Json along with multipart file in SpringBoot

I'm trying to figure out how to pass a collection of optional parameters to a SpringBoot controller, along with a multipart file, but can't figure out how.

Without the multipart file, this works fine

public ResponseEntity<ViewModel> performOperationOnFilePath(@Valid @RequestBody final FileOperationFileContract requestBody) 

where FileOperationFileContract is a POJO that looks like this

public class FileOperationFileContract {
    @NotBlank(message = "The filepath is required")
    private String filePath;

    private Map<String, String> options = new HashMap<>();

    @JsonAnyGetter
    public Map<String, String> getOptions() {
        return options;
    }

    @JsonAnySetter
    public void setOptions(final String name, final String value) {
        options.put(name, value);
    }
}

Here's what I tried with the multipart file

public ResponseEntity<ViewModel> performOperationOnFile(@RequestParam(name="myfile") final MultipartFile file, @RequestPart(name="options", required=false) final FileOperationMultipartFileContract requestBody)

where FileOperationMultipartFileContract is a POJO that looks like this

public class FileOperationMultipartFileContract {
    private Map<String, String> options = new HashMap<>();

    @JsonAnyGetter
    public Map<String, String> getOptions() {
        return options;
    }

    @JsonAnySetter
    public void setOptions(final String name, final String value) {
        options.put(name, value);
    }
}

This seems to work if I specify the Json payload in Postman like this (note that I must specify the content-type as application/json, or I get the message "'application/octet-stream' not supported"

enter image description here

But if there is no json part sent, I get a 403. I want the option collection to just be empty or null. In other words, I want it to default to {}

I've tried RequestPart and RequestParam, but there's no difference. I've also tried it with and without the @Valid annotation

How can I do this?

Update: I think that I figured out that my problem does not have as much to do with Spring as it does with what I'm trying to do with the @JsonAnyGetter and @JsonAnySetter annotations. If I change FileOperationFileContract to have regular fields, it works. But my guess is that the way I have it doesn't work very well with null. Although if the value is null, there should be no calls to setOptions. Any ideas?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...