@ControllerAdvice
public class EpExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Map<String, Object> allExceptionHandler(Exception e){
Map<String, Object> map = new HashMap<>(2);
if(e instanceof BindException) {
BindException ex = (BindException)e;
BindingResult bindingResult = ex.getBindingResult();
StringBuilder errMsg = new StringBuilder(bindingResult.getFieldErrors().size() * 16);
errMsg.append("Invalid request:");
for (int i = 0 ; i < bindingResult.getFieldErrors().size() ; i++) {
if(i > 0) {
errMsg.append(",");
}
FieldError error = bindingResult.getFieldErrors().get(i);
errMsg.append(error.getField()+":"+error.getDefaultMessage());
}
map.put("errcode", 500);
map.put("errmsg", errMsg.toString());
}else {
e.printStackTrace();
map.put("errcode", 500);
map.put("errmsg", e.getMessage());
}
return map;
}
}