[英]Error 400 Spring JSON @RequestBody
I'm facing an error 400 when trying to post data to Spring Controller using @RequestBody 尝试使用@RequestBody将数据发布到Spring Controller时遇到错误400
My Ajax method: 我的Ajax方法:
$(document).on('click', 'a[href|=#undo]', function () {
var i = $(this).attr('data-ref');
var uri = '/catalog/uso/undo/' + i;
var data = {"quantidade":$('.qntundo').val()};
console.log(JSON.stringify(data));
$.ajax({
url: uri,
type: 'post',
data: JSON.stringify(data),
dataType: 'json',
headers: {
"Content-Type": "application/json"
},
async: false,
......
data produces this : {"quantidade":"1"} 数据产生以下内容:{“ quantidade”:“ 1”}
and my method is like : 和我的方法是这样的:
@RequestMapping(value = "/catalog/uso/undo/{id}",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public
@ResponseBody
String undo(@PathVariable("id") int id,
@RequestBody Integer qnt) {
//some logic here
}
What I'm doing wrong, is that I cant use Integer as "body"? 我做错了,我不能将Integer用作“ body”吗? Or I need tell to @RequestBody look for "quantidade", if is that how can I do this?
或者我需要告诉@RequestBody寻找“ quantidade”,如果那我该怎么做?
PS: I have Jackson lib on my pom.xml also have PS:我的pom.xml上有Jackson lib
<mvc:annotation-driven/>
<mvc:default-servlet-handler/>
enabled in my Spring XML 在我的Spring XML中启用
I worked around with the tips in comments the result is I changed the type of annoted field on method making that: 我处理了注释中的提示,结果是我更改了使方法添加注释的字段的类型:
@RequestMapping(value = "/catalog/uso/undo/{id}",
method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public
@ResponseBody
String undo(@PathVariable("id") int id,
@RequestBody LinkedHashMap qnt) {
Now I have a LinkedHashMap comming and I can access it to get the value, like: 现在我有一个LinkedHashMap来了,我可以访问它来获取值,例如:
Object o = qnt.get("qnt");
where .get("your json object name here");
.get("your json object name here");
I was needing an Integer so I did a conversion: 我需要一个整数,所以进行了转换:
int quantidade = Integer.valueOf(String.valueOf(o));
Now I have access to value of json within the variable quantidade
现在我可以访问变量
quantidade
的json值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.