【问题标题】:Content type 'application/x-www-form-urlencoded' not supported" for post methodpost方法的内容类型'application / x-www-form-urlencoded'不支持”
【发布时间】:2019-11-13 02:31:05
【问题描述】:
@RestController
@RequestMapping("/user")
public class UserController {

    @PostMapping(value = "/",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<InternalUser> saveInternalUser(@RequestBody InternalUserDTO dto){

       InternalUser user =  internalUserService.saveInternalUser(dto);
        return new ResponseEntity<>(user, HttpStatus.CREATED);
    }

这是我的课。当我使用带有有效 json 的邮递员到该网址时 http://localhost:8090/user

我得到错误

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported]

我试过这个 https://stackoverflow.com/a/38252762/11369236

我不想在这里使用 requestparam

https://stackoverflow.com/a/43065261/11369236

一些答案​​建议使用

          consumes = MediaType.APPLICATION_JSON,

但是spring给出了错误:

attribute must be constant

我能做什么?

【问题讨论】:

    标签: spring http


    【解决方案1】:

    您必须在请求中添加 Content-type: application/json;charset=utf-8 标头。

    默认情况下(如果没有Content-type 标头)POST 请求正文的内容类型是application/x-www-form-urlencoded。 在@RequestMapping by consumes = MediaType.APPLICATION_JSON_UTF8_VALUE 中提到请求正文中需要UTF-8 编码中的json。这就是spring抛出异常的原因。

    【讨论】:

      【解决方案2】:

      使用 body > raw 为 API 请求发送 JSON 数据

      {
          "name": "John Doe"
          "email": "xyz@abc.com"
      }
      

      POSTMAN-POST-REQUEST

      【讨论】:

      • 能否请您发送 POSTMAN 的 Header 和 BODY 选项卡屏幕截图?以便我可以查看和分析更多内容
      猜你喜欢
      • 1970-01-01
      • 2022-01-19
      • 2016-02-21
      • 2020-04-13
      • 2020-04-21
      • 2021-02-03
      • 1970-01-01
      • 2015-04-02
      • 2020-02-02
      相关资源
      最近更新 更多