A description

  1. Can use enumerations
  2. Familiar with section-oriented programming ideas
  3. Be familiar with the enumeration
  4. Familiar with abnormal
  5. Basic knowledge of SpringBoot
  6. Be familiar with basic project setup and omit this step in the presentation
  7. Springboot 2.0, JDK1.8, build tool IDEA

Custom exception classes

The author can only say that simple and clear direct lu code.

2.1 Importing dependencies into POM files

<dependencies> <! GroupId > <artifactId> Spring-boot-starter -web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> The < version > 1.16.18 < / version > < scope > provided < / scope > < / dependency > < / dependencies >Copy the code

2.2 Using enumerations to customize exception class messages

/* * @author LSC * @description <p> Abnormal message </p> * @date 2019/10/9 20:28 * @param * @returnPublic enum CodeMsg {SUCESS(200,"sucess"),
    SERVER_ERROR(500,"Server exception"),
    Request_Error(404,"Request exception"); Private Integer code; // Error message private String MSG; public IntegergetCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

2.3 Integrate RuntimeException with custom exceptions

/** * @author LSC * @description <p> Custom generic exception </p> * @date 2019/10/9 20:39 * @version 1.0 */ @noargsconstructor @allargsconstructor @data public Class CommonException extends RuntimeException {private static final Long serialVersionUID = 132719492019L; Private CodeMsg CodeMsg; }Copy the code

2.4 Write pages to return entity classes

/** * @author LSC * @description <p> * @date 2019/10/9 20:35 * @version 1.0 */ @data //setConstructor @noargsconstructor public class ResultPage<T> {// private Integer code; Private String MSG; Private T data; ResultPage(Integer code,String msg) { this.code=code; this.msg=msg; this.data=data; } public static <T> ResultPage<T> sucess(CodeMsg CodeMsg,T data){returnnew ResultPage<T>(codeMsg.getCode(),codeMsg.getMsg(),data); } public static <T> ResultPage<T> error(CodeMsg CodeMsg){returnnew ResultPage<T>(codeMsg.getCode(),codeMsg.getMsg()); }}Copy the code

2.5 Exception Catching

/* * @author LSC * @description <p> Exception caught </p> * @date 2019/10/9 20:42 * @param * @return**/ @controlleradvice @Configuration public class CommonExceptionHandler {// Catch CommonException @ExceptionHandler(value = CommonException.class) @ResponseBody public ResponseEntity<ResultPage> CommonExceptionHandler(CommonException e){// Get exception message CodeMsg CodeMsg = LLDB etCodeMsg(); // The setup error message page is returnedreturnResponseEntity.status(HttpStatus.OK).body(ResultPage.error(codeMsg)); } // The following can also be customized to catch other exceptions such as illegal parameter exceptions arithmetic exceptions and so on....... }Copy the code

2.6 the controller layer

/** * @author LSC * @description <p> Control layer </p> * @date 2019/10/9 20:57 * @version 1.0 */ @restController Public class YouKu1327Controller { @GetMapping("/youku1327/api/excetion")
    public ResultPage<Object> testException(){throw new CommonException(codemsg.request_error); } /* * @author LSC * @description <p> Success message </p> * @date 2019/10/9 21:21 * @param [] * @return org.springframework.http.ResponseEntity
     **/
    @GetMapping("/youku1327/api/sucess")
    public ResponseEntity testSucess(){
        ArrayList<Object> list = new ArrayList<>();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("youku1327"."Welcome to blog and wechat.");
        list.add(hashMap);
        returnResponseEntity.ok().body(ResultPage.sucess(CodeMsg.SUCESS,list)); }}Copy the code

2.7 start the class

/** * @author LSC * @description <p> Custom exception startup class </p> * @date 2019/10/9 20:56 * @version 1.0 */ @springBootApplication public class ExceptionApplication { public static void main(String[] args) { SpringApplication.run(ExceptionApplication.class,args); }}Copy the code

Start the project page request test

3.1 Test Exceptions

3.2 Test Success