Java 不支持内容类型 'multipart/form-data;boundary=----...;charset=UTF-8'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48051177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Content type 'multipart/form-data;boundary=----...;charset=UTF-8' not supported
提问by sdafasdf
I want to send an object to the controller that has several lists with files and several fields with plain text.
我想向控制器发送一个对象,该对象有几个包含文件的列表和几个带有纯文本的字段。
public class ContributionNew<T extends MovieInfoDTO> {
private List<T> elementsToAdd;
private Map<Long, T> elementsToUpdate;
private Set<Long> idsToDelete;
private Set<String> sources;
private String comment;
}
public class Photo extends MovieInfoDTO {
private MultipartFile photo;
}
@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)
@RequestBody @Valid final ContributionNew<Photo> contribution
) {
I am sending data using postman. However, he throws me away
我正在使用postman发送数据。然而,他把我扔了
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=----WebKitFormBoundarywY7ByvgonAjDoaCT;charset=UTF-8' not supported
What should I set the Content-type for this controller so that I can send an object that has fields of plain text and lists with files?
我应该为这个控制器设置什么内容类型,以便我可以发送一个具有纯文本字段和文件列表的对象?
If I set the header in the header
如果我在标题中设置标题
Content-type: multipart/form-data; charset=utf-8
it throws me in the console
它把我扔进控制台
org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found
回答by plue
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
在邮递员中,您需要将正文设置为原始类型,然后从下拉列表中您可以选择 JSON,我遇到了类似的问题,这解决了我的问题。在此处查看屏幕
回答by uncleBounty
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.
正如 dknight @RequestBody 所说的,使用 JSON 或 XML 数据映射您的 DTO bean。在 MultipartFile 的情况下,您不能使用 JSON 数据,因此您不能使用 @RequestBody。尝试使用 @ModelAttribute 注释。
Working sample :
工作样本:
@PostMapping("/promoters")
@Timed
public ResponseEntity<PromoterDTO> createPromoter(@ModelAttribute PromoterDTO promoterDTO) throws URISyntaxException { ... }
With PromoterDTO like this :
像这样的推广者DTO:
public class PromoterDTO implements Serializable {
private Long id;
private String name;
private String address;
private MultipartFile logo;
}
回答by Chirag Shah
Instead of @RequestBody, use @ModelAttribute like,
而不是@RequestBody,使用@ModelAttribute 之类的,
@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
) {
回答by Shadab Reza
Instead of @RequestBody, use @RequestParam!!!
而不是@RequestBody,使用@RequestParam!!!
回答by user8364607
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!
使用 @ModelAttribute 而不是 @ResponseBody,因为它以键值对的形式占用数据,而后者用于像 json 这样的对象。在点击 api 时,只需传递对象的多部分类型和 json 键值对。它工作正常!