我试图让Spring 3.2 MVC返回没有默认标签的JSON响应。
例如,
@Controller
@RequestMapping("/dt")
public class DTAgentsController {
@ModelAttribute
@RequestMapping(method = RequestMethod.GET, produces = "application/json;UTF-8")
public DTResponse agents() {
DTResponse resp = new DTResponse();
resp.setsEcho(1);
resp.setiTotalDisplayRecords(10);
resp.setiTotalRecords(50);
return resp;
}
}
返回
{"DTResponse":{"sEcho":1,"iTotalRecords":50,"iTotalDisplayRecords":10}}
我只想要JSON输出
{"sEcho":1,"iTotalRecords":50,"iTotalDisplayRecords":10}
感谢。
答案 0 :(得分:0)
问题不在于@ModelAttribute
,它只是意味着要存储或从模型中获取的数据。您似乎使用了jQuery数据表,因此您应该将 @ResponseBody
添加到方法agents()
。
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public DTResponse agents() {
DTResponse resp = new DTResponse();
resp.setsEcho(1);
resp.setiTotalDisplayRecords(10);
resp.setiTotalRecords(50);
return resp;
}
}