问题描述
我想POST JSON并让Jackson将其转换为POJO,但是现在我甚至不能发布一个简单的String参数。
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.
-
我正在使用Jackson 2.3.2(jackson-core和jackson-databaind)
I'm using Jackson 2.3.2 (jackson-core and jackson-databaind)
我正在使用Spring 3.2。 8.RELEASE(spring-context,spring-webmvc)
I'm using Spring 3.2.8.RELEASE (spring-context, spring-webmvc)
我的控制器如下所示:
@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";
}
我用这样的数据POST到这个控制器:
I POST to this Controller with data like this:
{ "name" : "my_name" }
我收到的回复是 400错误请求
注意:我通过,但要删除它作为变量,我也通过指定URL,内容类型(应用程序/ json)和上述数据。
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.
编辑:我认为问题可能是杰克逊图书馆甚至没有被召集。我把它从依赖项中删除,没有任何变化,没有不同的错误/异常,我仍然只是得到 400 Bad Request
响应。
I think the issue may be that the Jackson library isn't even being called. I took it out of the dependencies and nothing changes, no different error/exception, I still simply get a 400 Bad Request
response.
编辑2:即使我将参数完全取出到 addName
并发布空体 { }
我仍然得到 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
推荐答案
我降级到Spring 3.1.1.RELEASE和Jackson 1.9.9并且现在可以使用了。好像是一个黑客。我认为Spring 3.2和Jackson 2兼容。
I downgraded to Spring 3.1.1.RELEASE and Jackson 1.9.9 and it works now. Seems like a hack. I thought Spring 3.2 and Jackson 2 were compatible.
这篇关于将JSON发布到Spring MVC Controller返回400 Bad Request的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!