1.创建一个异常:
public class LdapQueryException extends Exception {
private Integer code;
private String message; public LdapQueryException(Integer code, String message) {
super(message);
this.code = code;
this.message = message;
} public Integer getCode() {
return code;
} public void setCode(Integer code) {
this.code = code;
} @Override
public String getMessage() {
return message;
} public void setMessage(String message) {
this.message = message;
}
}
2.写一个异常处理器 适应注解 @ControllerAdvice 植入
@ControllerAdvice
public class CommonExceptionHandler {
@ResponseBody
@ExceptionHandler(value = LdapQueryException.class) //value 表示处理哪个异常
public Map ldapQueryHandler(LdapQueryException exp){
Map map=new HashMap();
map.put("code",exp.getCode());
map.put("message",exp.getMessage());;
return map;
}
}
3.使用: throw new LdapQueryException(500, "系统错误")