不支持 Spring/Postman 内容类型“应用程序/八位字节流”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50395010/
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
Spring/Postman Content type 'application/octet-stream' not supported
提问by StackJP
I'm using Postman to send the following request:

My controller looks like this:
我的控制器看起来像这样:
@RestController
@RequestMapping(path = RestPath.CHALLENGE)
public class ChallengeController {
private final ChallengeService<Challenge> service;
@Autowired
public ChallengeController(ChallengeService service) {
this.service = service;
}
@ApiOperation(value = "Creates a new challenge in the system")
@RequestMapping(method = RequestMethod.POST, consumes = {MediaType.MULTIPART_FORM_DATA_VALUE, MediaType.APPLICATION_OCTET_STREAM_VALUE},
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseStatus(HttpStatus.CREATED)
public ChallengeDto create(@ApiParam(value = "The details of the challenge to create") @RequestPart("challengeCreate") @Valid @NotNull @NotBlank ChallengeCreateDto challengeCreate,
@ApiParam(value = "The challenge file") @RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
return service.create(challengeCreate, file);
}
}
I already tried to change the "consumes" to delete the APPLICATION_OCTET_STREAM_VALUE to MULTIPART_FORM_DATA_VALUE and also tried to delete it, but none of these helped.
我已经尝试更改“消耗”以将 APPLICATION_OCTET_STREAM_VALUE 删除为 MULTIPART_FORM_DATA_VALUE 并尝试删除它,但这些都没有帮助。
Please tell me if you need more information. Thanks.
如果您需要更多信息,请告诉我。谢谢。
回答by Rokin
For Spring's @RequestPart to work with json objects, in Postman - you need to send the json object as a Fileinstead of Text.
对于 Spring 的 @RequestPart 使用 json 对象,在 Postman 中 - 您需要将 json 对象作为File而不是Text 发送。
Put the contents of ChallengeCreateDtoin a json file and save it as challenge.json. Then upload this file in Postman with the type as File.
I've attached a screenshot of how the request in Postman should be just to make it more clearer.
![Postman Request Screenshot][1]][1](/static/img/viewimg.png)
将ChallengeCreateDto的内容放在一个 json 文件中,并保存为challenge.json。然后在 Postman 中上传这个文件,类型为File。我附上了一张 Postman 中的请求的截图,只是为了让它更清晰。
![邮递员请求截图][1]][1](/static/img/viewimg.png)
You can also use @PostMapping instead of @RequestMapping in the newer versions of Spring as shown here
您还可以在较新版本的 Spring 中使用 @PostMapping 而不是 @RequestMapping,如下所示
@ApiOperation(value = "Creates a new challenge in the system")
@ResponseStatus(HttpStatus.CREATED)
@PostMapping()
public ChallengeDto create(@ApiParam(value = "The details of the challenge to create") @RequestPart("challengeCreate") @Valid @NotNull @NotBlank ChallengeCreateDto challengeCreate,
@ApiParam(value = "The challenge file") @RequestPart("file") @Valid @NotNull @NotBlank MultipartFile file) {
return service.create(challengeCreate, file);
}
[1]: https://i.stack.imgur.com/rpG2H.png
回答by Piyush_Chandra
Using @RequestParam to get string and file will solve this issue. In Postman use Content-Type as "multipart/form-data" and in Body define your input as form-data.
使用@RequestParam 获取字符串和文件将解决此问题。在 Postman 中使用 Content-Type 作为“multipart/form-data”,在 Body 中将您的输入定义为 form-data。
Refer https://stackoverflow.com/a/38336206/1606838
参考https://stackoverflow.com/a/38336206/1606838
Example:
例子:
@PostMapping(consumes = {"multipart/form-data"})
public Output send(@RequestParam String input, @RequestParam MultipartFile file) {
}
回答by Irfan Nasim
@Rokin Answer is good but dont need to place json in file and upload. You can achieve by passing content type to json object as well. Postman support content type options while sending form-data. for further see the below image which is self descriptive.
@Rokin 答案很好,但不需要将 json 放在文件中并上传。您也可以通过将内容类型传递给 json 对象来实现。邮递员在发送表单数据时支持内容类型选项。进一步参见下面的自我描述图像。
回答by John
Content-Type is a header setting and by default it is not checked and defaults to application-octet-stream. Just check the box under the headers ribbon item (which once checked defaults to application/json). 
Content-Type 是标头设置,默认情况下未选中,默认为 application-octet-stream。只需选中标题功能区项下的框(选中后默认为 application/json)。

