SpringBoot the code clean way one of the series: global exception handler

introduction

Online see this sentence is very interesting to share to you, this sentence is: you wrote the code hidden you read the book, seen the scenery, walked the streets, listening to music and loved people . Although it is a joke, but it also reflects the clean and elegant code that you write for self-demands and encouragement.

Benpian from the start, I will write about the use of SpringBoota series of articles the development of the project, how to clean the code of practice. Bad code is not only BUGone hundred, is not conducive to team development and expansion. Therefore, the preparation of clean and elegant code can not only provide the efficiency of development teams also conducive to future expansion. Less coding, more thinking Think more, code less

  • Global exception handler
  • to sum up

First, the global exception handler

1, using the handle exceptions @RestControllerAdvice annotation
(1) defined exception class

public class ProgramException extends RuntimeException{

    //错误码
    private String code;

    //错误信息
    private String msg;

    public ProgramException(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

(2) define the global response object

public class ReponseData {

    //错误码
    private String code;

    //错误信息
    private String msg;

    //响应数据
    private Object data;

    public ReponseData(String code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    
  	public ReponseData(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

(3) define the global exception handler

@RestControllerAdvice
public class BusinessServiceExceptionHandler {
private Logger logger = LoggerFactory.getLogger(BusinessServiceExceptionHandler.class);
	
	@ExceptionHandler(ProgramException.class)
	@ResponseStatus(ProgramException.class)
	public ResponseData handleException(ProgramException e, HttpServletRequest req){
	return new ResponseData(e.getCode(), e.getMsg());
	}
}

Comparative recommended to use this method, simple, especially now front end of the separation, the rear end of the interface to provide a unified Restful front end, if an exception occurs, the error message may be returned by a unified exception handler.

2, using the global exception handling SimpleMappingExceptionResolver

@Configuration
public class GlobalExceptionResolver extends SimpleMappingExceptionResolver{
	
private static Logger log = LoggerFactory.getLogger(GlobalExceptionResolver.class);
	
	public GlobalExceptionResolver() {
		setOrder(0);
	}

	

Second, summary

Clean code includes not only the actual project business code, including some system-level. Global exception handler belonging to the system-level code cleanliness. If we made our project more than a city, then a similar global exception handler, interceptors and so is equivalent to the city's drainage system, cable management, and so on. These facilities arranged a direct impact on the functioning of the entire city. Then the project level, the system level and clean the code is to make the project can run more efficiently.

发布了88 篇原创文章 · 获赞 49 · 访问量 10万+

Guess you like

Origin blog.csdn.net/Diamond_Tao/article/details/93981488