通常のリクエストと残り/ajaxリクエストの両方で例外を処理したい。ここに私のコードがあります、
@ControllerAdvice
public class MyExceptionHandler {
@ExceptionHandler(Exception.class)
public ModelAndView handleCustomException(Exception ex) {
ModelAndView model = new ModelAndView("error");
model.addObject("errMsg", ex.getMessage());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
sw.toString();
model.addObject("errTrace", sw);
return model;
}
@ExceptionHandler(Exception.class)
@ResponseBody
public String handleAjaxException(Exception ex) {
JSONObject model = new JSONObject();
model.put("status", "error");
model.put("errMsg", ex.getMessage());
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
sw.toString();
model.put("errTrace", sw);
return model.toString();
}
}
@ExceptionHandler(Exception.class) を2回使用できないため、これによりエラーが発生します。では、解決策は何でしょうか?