springboot receives complex parameters (receives JSON and files at the same time)

We usually use @RequestBody to receive json, and @RequestParam to upload files. If you want to upload both at the same time, you can’t do it with the previous annotation. You can use @RequestPart instead.

code example

@PostMapping(value = "test")
    public R test(@RequestPart TestData data, MultipartFile file){
    
    
        log.info("数据{},文件{}",data,file.getOriginalFilename());
        return R.ok().data("data",data);
    }

insert image description here

Pay attention to the method of passing parameters. The Content-Type must be set for the json string. Application/json does not work in some scenarios, so you can use application/problem+json.

insert image description here
@RequestPart and @RequestParam are more powerful than the latter. They support both multipart/form-data and parameters in json, xml and other formats, which means that the key in the multipart/form-data format can be in various formats.

Guess you like

Origin blog.csdn.net/worilb/article/details/127903172