HTTP response 添加body

在拦截器中进行拦截操作时,想要给response添加body,如何操作?

    /**
     * 返回JSON数据
     * @param response
     * @param obj
     * @throws Exception
     */
    public static void responseJson(HttpServletResponse response, Object obj) throws Exception {
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print(JSONObject.toJSONString(obj, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat));
        writer.close();
        response.flushBuffer();
    }

调用

responseJson(response, new APIRespJson(resultCode));

 

posted @ 2018-05-18 09:58  Rainyn  阅读(1475)  评论(0编辑  收藏  举报