【问题标题】:Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported内容类型 'application/x-www-form-urlencoded;charset=UTF-8' 不支持
【发布时间】:2020-05-13 12:33:32
【问题描述】:

@PostMapping public UserResponse createUser(@RequestBody UserRequest userDetail) {

    UserResponse returnValue = new UserResponse();
    UserDto userDto = new UserDto();

    BeanUtils.copyProperties(userDetail, userDto);

    UserDto storedData = userService.createUser(userDto);
    BeanUtils.copyProperties(storedData, returnValue);

    return returnValue;
}

这是我收到此错误的代码

{
"timestamp": "2020-05-13T12:24:04.866+0000",
"status": 415,
"error": "Unsupported Media Type",
"message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
"path": "/users"

}

我尝试了很多不同的方法仍然没有得到解决方案 这是邮递员的图片 image from postman

【问题讨论】:

    标签: java spring


    【解决方案1】:

    您是否使用 Postman 来触发请求?您可能需要检查这些设置

    编辑。添加另一个图像以进行故障排除

    【讨论】:

    • 但仍然得到相同的结果
    • 你能给我看看你是如何创建你的请求的屏幕截图吗?
    • 我已经添加了图片
    • 下拉“JSON”旁边的黄色三角形是什么意思?
    • 消息说:“选择的语言与从 content_type 标头推断的语言不同”
    【解决方案2】:

    那是因为您的服务只接受 application/json 内容类型。如果您想接受 application/x-www-form-urlencoded 您可以在 @PostMapping 注释中添加 consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE

    下面是接受 application/json 并给出 application/json 响应的服务示例。

    @PostMapping(path = "/v1/agent/create", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
        public ApiResponseWrapperDto createAgent(@RequestBody ApiRequestWrapperDto request) throws Exception {
            return this.agentManagementApiHandler.createAgent(request);
    }
    

    希望这能解决您的问题。

    谢谢

    【讨论】:

    • 问题依旧
    猜你喜欢
    • 2022-01-19
    • 2016-02-21
    • 2020-04-13
    • 2019-11-13
    • 2018-07-24
    • 2015-11-01
    • 2015-03-26
    • 1970-01-01
    • 2020-04-21
    相关资源
    最近更新 更多