1.自定义异常
package com.sun.execption;
public class AuthException extends RuntimeException {
public AuthException(String message) {
super(message);
}
}
2.异常类
package com.sun.execption;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice //表示切面类
public class SysHandlerException {
@ExceptionHandler(AuthException.class)
@ResponseBody
public Map authExceptionHandler(Exception e){
Map map = new HashMap();
map.put("status",501);
map.put("message--auth",e.getMessage());
return map;
}
@ExceptionHandler(Exception.class)
@ResponseBody
public Map exceptionHandler(Exception e){
Map map = new HashMap();
map.put("status",500);
map.put("message--exception",e.getMessage());
return map;
}
}