Spring Boot - Multipart - Unsupported Media Type

前端 未结 1 766
独厮守ぢ
独厮守ぢ 2021-01-20 17:38

I want to send a file and a json model at one post request.

My Request Mapping looks like that:

    @PostMapping(\"{id}/files\")
    public MyOutput          


        
相关标签:
1条回答
  • 2021-01-20 18:35

    You can try to use instead of this

    @RequestPart("file") MultipartFile file
    

    use this

    @RequestParam(value = "file",required = false) MultipartFile file
    

    And be sure you set the request type as multipart/form-data You can set it from postman in the headers tab.

    If another object you need to send with multipart file,you can send it as a string and then you can convert it to object at the backend side.

      @PostMapping("/upload")
        public void uploadFile(@Nullable @RequestParam(value = "file",required = false) MultipartFile file,
                                                @RequestParam(value = "input", required = false) String st)
        {
            ObjectMapper om = new ObjectMapper();
            MyInput input = null;
            try {
                input = om.readValue(st, MyInput.class);   //string st -> MyInput input
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    Postman request example :

    0 讨论(0)
提交回复
热议问题