java - Spring 中的内容类型问题

标签 java spring http-headers

我收到的帖子有问题。我有以下端点:

@RequestMapping(value = "/payment", method = POST)
public void saveOrder(@RequestBody PaymentDto paymentDto) throws RequiredFieldException, IOException, MessagingException {
//do something
}

现在,当有人通过此 URL 向我发送 POST 时,我会收到以下响应:

{"errorMessage":"Unsupported Media Type",
"errorId":"906f5dc8-0b79-4f91-9eaa-a252e8d5ac76",
"errorDetails":
    {"message":"Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
    "exception":"org.springframework.web.HttpMediaTypeNotSupportedException",
    "errors":null
}}

我该如何解决这个问题?发送时我无法更改内容类型 header 。它是从我无法控制的服务器发送的。

最佳答案

如果 Content Type 是 application/json 或 application/xml 使用 @RequestBody 注解,如果是 application/x-www-form-urlencoded 使用 @ModelAttribute

关于java - Spring 中的内容类型问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39385960/

相关文章:

http-headers - 覆盖缓存控制 : Private in Nginx

java - Struts2-Rest : Manual HTTP 5xx doesn't return content

java - CDI 注入(inject)未在 Jackson 序列化程序中解决

java.io.EOFException : Unexpected end of ZLIB input stream using Apache POI

java - 如何在没有 XML 的情况下更改 ListView 背景和文本颜色

java - Freemarker 日期分配看似有效日期失败

spring - 使用 MongoDB hibernate

java - 创建表单时出现 Spring 错误

angular - 预检响应具有无效的 http 状态代码 401?

java - Gson:如何更改枚举的输出