2013-03-16 87 views
1

我试图让Spring 3.2 MVC返回一个没有默认标签的JSON响应。Spring 3.2 Web MVC @ModelAttribute without label

例如,

@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

问题不在于@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; 
    } 
} 
+0

谢谢:)。奇迹般有效。 – user2176499 2013-03-16 12:32:04

相关问题