【问题标题】:Handle Unsupported Media Types in Spring在 Spring 中处理不支持的媒体类型
【发布时间】:2020-08-26 19:34:46
【问题描述】:

我目前正在使用 Spring 框架构建一个简单的 REST 服务。但是在使用 Postman 进行测试时,我遇到了以下问题: Click to view image

我想知道如何让 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:

My Endpoint image

【问题讨论】:

  • 请用您的休息终点更新您的问题
  • 不要添加图片,添加你的代码

标签: spring rest http-headers mime-types


【解决方案1】:

试试这个:

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

【讨论】:

  • 不幸的是,这对我不起作用。仍然抛出不支持的媒体类型异常。
猜你喜欢
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 2013-08-08
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多