Content type 'multipart/form-data;boundary=----…;charset=UTF-8' not supported

后端 未结 5 2275
误落风尘
误落风尘 2021-01-01 21:35

I want to send an object to the controller that has several lists with files and several fields with plain text.

public class ContributionNew

        
相关标签:
5条回答
  • 2021-01-01 21:45

    As said dknight @RequestBody means use of JSON or XML data with maps your DTO bean. In case of MultipartFile you can't use JSON data so you can't use @RequestBody. Try with @ModelAttribute annotation.

    Working sample :

    @PostMapping("/promoters")
    @Timed
    public ResponseEntity<PromoterDTO> createPromoter(@ModelAttribute PromoterDTO promoterDTO) throws URISyntaxException { ... }
    

    With PromoterDTO like this :

        public class PromoterDTO implements Serializable {
    
            private Long id; 
    
            private String name;
    
            private String address;
    
            private MultipartFile logo;
        }
    
    0 讨论(0)
  • 2021-01-01 21:46

    use @ModelAttribute instead of @ResponseBody as this takes up data in key value pairs and the later is used for an object like, json. While hitting the api simply pass the multipart type and json key value pairs of the object. It works fine!

    stack overflow question on this

    0 讨论(0)
  • 2021-01-01 21:48

    Instead of @RequestBody, use @ModelAttribute like,

    @PostMapping(value = "/{id}/contributions/photos")
    @ResponseStatus(HttpStatus.CREATED)
    public
    ResponseEntity<Void> createPhotoContribution(
            @ApiParam(value = "The movie ID", required = true)
            @PathVariable("id") final Long id,
            @ApiParam(value = "The contribution", required = true)
            @ModelAttribute @Valid final ContributionNew<Photo> contribution
    ) {
    
    0 讨论(0)
  • 2021-01-01 21:49

    Instead of @RequestBody, use @RequestParam!!!

    0 讨论(0)
  • 2021-01-01 22:04

    In Postman, you need to set the body to be of type raw, and from the drop down you can select JSON, I had a similar issue, this fixed my issue.

    view screen here

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