Spring的拦截器中经常会用到HttpServletRequest和HttpServletResponse
这里我遇到的一个情况是:
如果用户的请求是查询接口,且鉴权过不了的情况下,我需要返回空List
/**
* 以JSON格式输出
*
* @param response
* @param responseObject
*/
public void responseOutWithJson(HttpServletResponse response, Object responseObject) {
//将实体对象转换为JSON Object转换
JSONObject responseJSONObject = JSONObject.parseObject(responseObject.toString());
response.setCharacterEncoding("UTF-8");
response.setContentType("application/json; charset=utf-8");
try {
response.getWriter().append(responseJSONObject.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
这样的话在拦截器返回false的情况下,response会输出你定义的内容,我这里就是格式化后的对象 jo