@RequestBody MultiValueMap 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8" [英] Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported for @RequestBody MultiValueMap

查看:53
本文介绍了@RequestBody MultiValueMap 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基于答案 x-www-form-使用 Spring @Controller 进行 urlencoded

我写了下面的@Controller 方法

I have written the below @Controller method

@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST
            , produces = {"application/json", "application/xml"}
            ,  consumes = {"application/x-www-form-urlencoded"}
    )
     public
        @ResponseBody
        Representation authenticate(@PathVariable("email") String anEmailAddress,
                                    @RequestBody MultiValueMap paramMap)
                throws Exception {


            if(paramMap == null || paramMap.get("password") == null) {
                throw new IllegalArgumentException("Password not provided");
            }
    }

请求失败并出现以下错误

the request to which fails with the below error

{
  "timestamp": 1447911866786,
  "status": 415,
  "error": "Unsupported Media Type",
  "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
  "message": "Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported",
  "path": "/users/usermail%40gmail.com/authenticate"
}

[PS:Jersey 更友好,但鉴于这里的实际限制,现在不能使用它]

[PS: Jersey was far more friendly, but couldn't use it now given the practical restrictions here]

推荐答案

问题是当我们使用application/x-www-form-urlencoded时,Spring并没有把它理解为一个RequestBody.所以,如果我们想使用这个我们必须删除 @RequestBody 注释.

The problem is that when we use application/x-www-form-urlencoded, Spring doesn't understand it as a RequestBody. So, if we want to use this we must remove the @RequestBody annotation.

然后尝试以下操作:

@RequestMapping(value = "/{email}/authenticate", method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, 
        produces = {MediaType.APPLICATION_ATOM_XML_VALUE, MediaType.APPLICATION_JSON_VALUE})
public @ResponseBody  Representation authenticate(@PathVariable("email") String anEmailAddress, MultiValueMap paramMap) throws Exception {
   if(paramMap == null && paramMap.get("password") == null) {
        throw new IllegalArgumentException("Password not provided");
    }
    return null;
}

注意删除了注解@RequestBody

答案:内容类型为 application/x-www-form-urlencoded 的 Http Post 请求在 Spring 中不起作用

这篇关于@RequestBody MultiValueMap 不支持内容类型“application/x-www-form-urlencoded;charset=UTF-8"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆