我有问题
using Gzip compression and JQuery together.似乎这可能是由我的方式发送JSON响应在我的Struts操作.我使用下一个代码发回我的JSON对象.
public ActionForward get(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) { JSONObject json = // Do some logic here RequestUtils.populateWithJSON(response,json); return null; } public static void populateWithJSON(HttpServletResponse response,JSONObject json) { if(json!=null) { response.setContentType("text/x-json;charset=UTF-8"); response.setHeader("Cache-Control","no-cache"); try { response.getWriter().write(json.toString()); } catch (IOException e) { throw new ApplicationException("IOException in populateWithJSON",e); } } }
在Java Web应用程序中是否有更好的方式发送JSON?
解决方法
代替
try { response.getWriter().write(json.toString()); } catch (IOException e) { throw new ApplicationException("IOException in populateWithJSON",e); }
尝试这个
try { json.write(response.getWriter()); } catch (IOException e) { throw new ApplicationException("IOException in populateWithJSON",e); }
因为这将避免创建一个字符串,并且JSONObject将直接将字节写入Writer对象