永远不要自己拼装json
今天项目出现一个非常怪的现象,使用spring拦截的异常有时候显示不出来的问题,现在将原因和解决方案记录下,spring的拦截代码如下:
if(accept.indexOf("application/json")>-1){return super.getModelAndView("errors/ajax/500", ex, request);}<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>{"errorCode":-1,"errorMsg":"${exception.message}"}{"errorCode":"-1","errorMsg":"错误代码:-20002错误信息:ORA-20002: 3103: 项目 CUXECSWF/10888CUXECSWF120518150833 的属性 WF_NOTE 不存在。"}Map<String,String> errors = new HashMap<String,String>();errors.put("errorCode", "-1");errors.put("errorMsg", ex.getMessage());log.info(JSONObject.fromObject(errors).toString()){"errorCode":"-1","errorMsg":"错误代码:-20002\r错误信息:ORA-20002: 3103: 项目 CUXECSWF/10888CUXECSWF120518150833 的属性 WF_NOTE 不存在。"}Map<String,String> errors = new HashMap<String,String>();errors.put("errorCode", "-1");errors.put("errorMsg", ex.getMessage());request.setAttribute("errorJson", JSONObject.fromObject(errors).toString());return super.getModelAndView("errors/ajax/500", ex, request);<%@ page language="java" contentType="application/json; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>${errorJson}