方法一:對(duì)象不使用注解
@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) //稿件對(duì)象
使用Postman測(cè)試,直接將subject對(duì)象的字段填在key的位置
方法二:對(duì)象使用注解@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) //稿件對(duì)象
使用Postman測(cè)試,將字段包裝在subject對(duì)象里,使用Content type:application/json的內(nèi)容類型
注:方法二在開(kāi)發(fā)本地測(cè)試執(zhí)行成功,但是在測(cè)試人員機(jī)子下不通過(guò),執(zhí)行報(bào)錯(cuò)如下:Content type ‘a(chǎn)pplication/octet-stream’ not supported
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-664659.html
{
"timestamp": 1681976364292,
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/octet-stream' not supported",
"path": "/subject/syncDocuments"
}
原因:未將原始json格式的數(shù)據(jù)轉(zhuǎn)換為http能夠識(shí)別的字符串流。
解決方案:測(cè)試時(shí),可以把對(duì)象放到j(luò)son文件里,將.json文件上傳。(如果是前端,則需要轉(zhuǎn)成json格式)
使用Postman測(cè)試通過(guò)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-664659.html
到了這里,關(guān)于Java實(shí)現(xiàn)方法接口入?yún)⑼瑫r(shí)包含文件、字段和對(duì)象等多種類型。HTTP請(qǐng)求返回415狀態(tài),Content type ‘a(chǎn)pplication/octet-stream‘ not supported錯(cuò)誤。的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!