我有一个处理ajax调用和返回JSON的控制器方法。我使用来自json.org的JSON库来创建JSON。
我可以执行以下操作:
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public String getJson()
{
JSONObject rootJson = new JSONObject();
// Populate JSON
return rootJson.toString();
}
但是,将JSON字符串放在一起,只让Spring将其写入响应的输出流,效率很低。
相反,我可以将其直接写入响应输出流,如下所示:
@RequestMapping(method = RequestMethod.POST)
public void getJson(HttpServletResponse response)
{
JSONObject rootJson = new JSONObject();
// Populate JSON
rootJson.write(response.getWriter());
}
但似乎有一种更好的方法可以做到这一点,而不是将HttpServletResponse
传递给处理程序方法。
有没有其他类或接口可以从处理程序方法中返回,我可以使用它以及@ResponseBody
注释?
https://stackoverflow.com/questions/15283347
复制相似问题