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;
    }
}
View Code

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, "系统错误")

posted on 2018-10-22 14:26  lshan  阅读(176)  评论(0编辑  收藏  举报