How to send JSON back with JAVA?

后端 未结 4 2087
你的背包
你的背包 2021-02-14 05:04

I am having problems using Gzip compression and JQuery together. It seems that it may be caused by the way I am sending JSON responses in my Struts Actions. I use the next code

4条回答
  •  庸人自扰
    2021-02-14 05:41

    Instead of

    try {
           response.getWriter().write(json.toString());
    } catch (IOException e) {
           throw new ApplicationException("IOException in populateWithJSON", e);
    }        
    

    try this

    try {
            json.write(response.getWriter());
    } catch (IOException e) {
            throw new ApplicationException("IOException in populateWithJSON", e);
    }                                      
    

    because this will avoid creating a string and the JSONObject will directly write the bytes to the Writer object

提交回复
热议问题