json 获取不支持的媒体类型错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30511911/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:55:47  来源:igfitidea点击:

Getting not supported media type error

jsonrestspring-boot

提问by Qasim

Hi i am using Rest client i.e Postman to send the REST request but getting error :

嗨,我正在使用 Rest 客户端,即 Postman 发送 REST 请求,但出现错误:

{
    "timestamp": 1432829209385,
    "status": 415,
    "error": "Unsupported Media Type",
    "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
    "message": "Content type 'text/plain;charset=UTF-8' not supported",
    "path": "/api/v1/user"
}

My controller is :

我的控制器是:

@RequestMapping(value = "/user", method = RequestMethod.PUT, produces = "application/json")
    public Map<String,Object> updateUser(@RequestBody @Valid User user) {
//      userService.updateUser(user);
        return ResponseHandler.generateResponse("", HttpStatus.ACCEPTED, false, null);
    }

I am sending the request as shown in picture via REST client.

我正在通过 REST 客户端发送如图所示的请求。

enter image description here

在此处输入图片说明

回答by kjsebastian

Change your Content-Typein Postman to application/json. Click on the Headersbutton to do so.

将您Content-Type的 Postman更改为application/json. 单击标题按钮执行此操作。

Also you are not producing json in the method. Remove the produces="application/json"from the annotation

您也没有在该方法中生成 json。produces="application/json"从注释中删除

回答by tom.g.c

I had the same issue, and resolved it in a different way.

我遇到了同样的问题,并以不同的方式解决了它。

Newer versions of Postman have options in the "Body" section. If "raw" is chosen, then there is a drop down where "JSON (application/json)" works.

较新版本的 Postman 在“正文”部分有选项。如果选择“原始”,则有一个下拉菜单,“JSON (application/json)” 起作用。

POST Body raw : JSON

POST 正文原始:JSON

回答by Niraj

Under Headers in Postman app, type a Key as Content-Typeand set the value as application\json.

在 Postman 应用程序中的标题下,键入一个 Key asContent-Type并将值设置为application\json

enter image description here

在此处输入图片说明