关于其余部分:Spring / Postman内容类型“应用程序/八位字节流”不受支持

Spring/Postman Content type 'application/octet-stream' not supported

我正在使用邮递员发送以下请求:
enter image description here

我的控制器如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@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);
    }
}

我已经尝试更改"消耗"以将APPLICATION_OCTET_STREAM_VALUE删除为MULTIPART_FORM_DATA_VALUE,并且还尝试将其删除,但是这些都无济于事。

如果您需要更多信息,请告诉我。
谢谢。


为了让Spring的@RequestPart与json对象一起工作,在Postman中-您需要将json对象作为文件而不是文本发送。

将ChallengeCreateDto的内容放在json文件中,并将其另存为Challenge.json。 然后将此文件上传到Postman中,类型为File。
我已经附上了一个截图,其中显示了Postman中的请求应该如何使其更加清晰。
Postman Request Screenshot][1]][1

您也可以在较新版本的Spring中使用@PostMapping而不是@RequestMapping,如下所示

1
2
3
4
5
6
7
8
9
10
@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

使用@RequestParam获取字符串和文件将解决此问题。
在Postman中,将Content-Type用作" multipart / form-data",在Body中,将您的输入定义为form-data。

请参阅https://stackoverflow.com/a/38336206/1606838

例:

1
2
3
4
 @PostMapping(consumes = {"multipart/form-data"})
public Output send(@RequestParam String input, @RequestParam MultipartFile file) {

}