Java实现方法接口入参同时包含文件、字段和对象等多种类型。HTTP请求返回415状态,Content type ‘application/octet-stream‘ not supported错误。

方法一:对象不使用注解

	@PostMapping(value = "/subject/syncDocuments")
    @ResponseBody
    @ApiImplicitParam(paramType = "body", dataType = "Subject", name = "subject", value = "稿件")
    public Map<String, Object> syncDocuments(@RequestParam(value = "file", required = true) MultipartFile file,
                                             @RequestParam(value = "type" )Integer type,
                                             Subject subject) //稿件对象

使用Postman测试,直接将subject对象的字段填在key的位置
在这里插入图片描述

方法二:对象使用注解@RequestPart

	@PostMapping(value = "/subject/syncDocuments")
    @ResponseBody
    @ApiImplicitParam(paramType = "body", dataType = "Subject", name = "subject", value = "稿件")
    public Map<String, Object> syncDocuments(@RequestParam(value = "file", required = true) MultipartFile file,
                                             @RequestParam(value = "type" )Integer type,
                                             @RequestPart Subject subject) //稿件对象

使用Postman测试,将字段包装在subject对象里,使用Content type:application/json的内容类型
在这里插入图片描述
注:方法二在开发本地测试执行成功,但是在测试人员机子下不通过,执行报错如下:
Content type ‘application/octet-stream’ not supported

{
    "timestamp": 1681976364292,
    "status": 415,
    "error": "Unsupported Media Type",
    "message": "Content type 'application/octet-stream' not supported",
    "path": "/subject/syncDocuments"
}

原因:未将原始json格式的数据转换为http能够识别的字符串流。
解决方案:测试时,可以把对象放到json文件里,将.json文件上传。(如果是前端,则需要转成json格式)
在这里插入图片描述
使用Postman测试通过
在这里插入图片描述