本文介绍了Spring / Postman内容类型“应用程序/八位字节流”不受支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用邮递员发送以下请求:

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);
    }
}

我已经尝试过将 consumes删除将APPLICATION_OCTET_STREAM_VALUE转换为MULTIPART_FORM_DATA_VALUE并尝试将其删除,但这些方法均无济于事。

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.

请告诉我是否需要更多信息。
谢谢。

Please tell me if you need more information.Thanks.

推荐答案

要让Spring的@RequestPart在邮递员中使用json对象,您需要发送json对象作为文件而不是文本

For Spring's @RequestPart to work with json objects, in Postman - you need to send the json object as a File instead of Text.

ChallengeCreateDto 的内容放入json文件中,并将其另存为Challenge.json。然后将此文件上传到Postman中,其类型为文件
我附上了一张截图,其中显示了Postman中的请求应该只是为了使其更加清晰。

Put the contents of ChallengeCreateDto in 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.

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

You can also use @PostMapping instead of @RequestMapping in the newer versions of Spring as shown here

@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

这篇关于Spring / Postman内容类型“应用程序/八位字节流”不受支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 19:36