首页 > 解决方案 > 在 Spring 中处理不支持的媒体类型

问题描述

我目前正在使用 Spring 框架构建一个简单的 REST 服务。但是在使用 Postman 进行测试时,我遇到了以下问题: 点击查看图片

我想知道如何让 Spring 识别收到的 Content-Type 标头实际上与application/x-www-form-urlencoded受支持的 .

更新:

我目前正在localhost使用嵌入式 Tomcat 服务器运行我的应用程序。

我的终点简要包括:

  1. POST: /api/users- 用于创建新用户
  2. GET: /api/users- 用于列出用户
  3. GET: /api/users/{id}- 用于通过 id 检索用户

POST 端点的请求正文:

"email":"<your-email>",
"password:"<your-password>"

更新2:

我的端点图像

标签: springresthttp-headersmime-types

解决方案


尝试这个:

@PostMapping(path = "/api/users",consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ResponseEntity<String> createUser(@RequestBody User user)

推荐阅读