【发布时间】:2016-02-21 03:25:20
【问题描述】:
基于for problem with x-www-form-urlencoded with Spring @Controller的答案
我已经写了下面的@Controller方法
@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");
}
}
请求失败并出现以下错误
{
"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 更加友好,但由于这里的实际限制,现在无法使用它]
【问题讨论】:
-
你在@RequestBody 中添加了consumes = {"application/x-www-form-urlencoded"} 吗?
-
你是如何执行请求的?添加 (js,jquery, curl 或任何你使用的) 的代码。
-
我也有同样的问题。就我而言,我使用 jquery ajax 发布数据,数据为
JSON.stringify({"ordersToDownload":"00417002"} -
这是我使用的代码:
$.ajax({url:"/myurl", type:"POST", data: JSON.stringify({"someAttribute":"someData"}) })
标签: spring spring-mvc model-view-controller