[英]POSTing JSON to Spring MVC Controller Returns 400 Bad Request
I'd like to POST JSON and have Jackson convert it to a POJO, but for now I can't even POST a simple String parameter. 我想发布JSON并让Jackson将其转换为POJO,但是现在我什至无法发布简单的String参数。
I'm using Jackson 2.3.2 (jackson-core and jackson-databaind) 我正在使用Jackson 2.3.2(jackson-core和jackson-databaind)
I'm using Spring 3.2.8.RELEASE (spring-context, spring-webmvc) 我正在使用Spring 3.2.8.RELEASE(spring-context,spring-webmvc)
My Controller looks like this: 我的控制器如下所示:
@RequestMapping(value="add-name", method=RequestMethod.POST, headers = "Accept=application/json") public String addName(@RequestParam(value = "name", required = false) String name) { LOG.info(String.format("name: %s", name)); return "name"; }
I POST to this Controller with data like this: 我用以下数据发布到该控制器:
{ "name" : "my_name" }
The response I receive is 400 Bad Request
我收到的回复是
400 Bad Request
Note: I'm submitting the POST via jQuery.post() , but to remove that as a variable, I'm also submitting POST via FF Poster add-on specifying the URL, Content Type (application/json), and data as above. 注意:我通过jQuery.post()提交POST,但是要删除它作为变量,我也通过FF Poster附加组件提交POST, 并将 URL,内容类型(application / json)和数据指定为以上。
Edit: I think the issue may be that the Jackson library isn't even being called. 编辑:我认为问题可能是甚至没有调用Jackson库。 I took it out of the dependencies and nothing changes, no different error/exception, I still simply get a
400 Bad Request
response. 我从依赖项中删除了它,没有任何变化,没有不同的错误/异常,我仍然只是得到
400 Bad Request
响应。
Edit 2: Even if I take out the parameter to addName
completely and post an empty body {}
I still get a 400 Bad Request
编辑2:即使我完全删除参数
addName
并张贴一个空的正文{}
我仍然收到400 Bad Request
You are posting raw data in the body, but your controller is looking for a parameter named name
. 您将原始数据发布到主体中,但控制器正在寻找名为
name
的参数。 Either post with a parameter name
whose value is my_name
, or use @RequestBody
in the handler to pass in the JSON object. 可以使用参数
name
为my_name
,或在处理程序中使用@RequestBody
传递JSON对象。
I downgraded to Spring 3.1.1.RELEASE and Jackson 1.9.9 and it works now. 我降级到Spring 3.1.1.RELEASE和Jackson 1.9.9,现在可以工作了。 Seems like a hack.
好像是hack。 I thought Spring 3.2 and Jackson 2 were compatible.
我以为Spring 3.2和Jackson 2是兼容的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.