¿Cómo devolver a JSON con JAVA?
Tengo problemasusando compresión Gzip y JQuery juntos. Parece que puede deberse a la forma en que envío respuestas JSON en mis acciones de Struts. Uso el siguiente código para devolver mis objetos 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);
}
}
}
Existe una mejor manera de enviar JSON en una aplicación web Java?