import com.alibaba.fastjson.JSONException;
@Controller
public class AccessController {
* 异常控制
* @Title: handleOtherExceptions
* @Description: RequestBody用fastjson解析出错的统一处理
* @param @param ex
* @param @return
* @return ResponseEntity<Object>
* @throws
*/
@ExceptionHandler(JSONException.class)
public ResponseEntity<Object> handleOtherExceptions(JSONException e) {
Result result = new Result();
String eMsg = e.getMessage();
if (eMsg.contains("not match : - ")) {
result.setFailResultMsg("json数据格式不正确,没有匹配的 '}' 或 '\"'");
} else if (eMsg.contains("unclosed string")) {
result.setFailResultMsg("json数据格式不正确,没有闭合的 '\"'");
} else if(eMsg.contains("syntax error, ")) {
result.setFailResultMsg("json数据格式不正确: ");
} else if (eMsg.contains("can not cast to")) {
String[] msg = eMsg.split(" ");
result.setFailResultMsg("转换类型失败:值 '" + msg[7] + "' 不能转成 "
+ msg[4].replace(",", "") + " 类型");
} else {
result.setFailResultMsg("未知异常: " + eMsg);
}
return new ResponseEntity<Object>(result, HttpStatus.OK);
}
}