这是我的jquery ajax ajax请求
$.ajax({
url: "/some_url",
type: "GET",
contentType : "application/json; charset=utf-8",
async: false,
data: {"attribute_1":"some_value_1","attribute_2":"some_value_2"};,
cache: false,
success: function(response) {}
});
这是我的控制器方法
@RequestMapping(value = "/some_url", method = { RequestMethod.GET })
public String getDetails(HttpServletRequest request,@RequestBody CustomDTO customDTO) {
}
但是请求没有达到控制器方法并得到404错误?如果我在ajax和控制器级别发布它可以工作。我了解@RequestBody
适用于POST
仅限要求。
我的问题如何在spring mvc下的ajax GET
请求中将输入请求参数映射到域对象?
我是否已根据定义将每个参数定义为getDetails(HttpServletRequest request,@RequestParam String pName1, @RequestParam String pName2,...)
How to pass Json object from ajax to spring mvc controller?或者是否有更简洁的方法将它们包装在域对象
答案 0 :(得分:1)
将JSON.stringify()
隐藏数据用于Json值。
<强>码强>
data: ({queryData:JSON.stringify({"attribute_1":"some_value_1","attribute_2":"some_value_2"}}))
然后您将数据作为字符串参数发送到spring然后使用jacksonLibrary
进行转换 @RequestMapping(value = "/some_url", method = { RequestMethod.GET })
public String getDetails(HttpServletRequest request,
@RequestParam String queryData)
{
ObjectMapper myMapper = new ObjectMapper();
QueryData myQueryData = myMapper.readValue(queryData, CustomDTO.class);
//now you can access data...
myQueryData.getattribute_1();
myQueryData.getattribute_2();
}