Springboot integrated custom exception class (enterprise combat)

A note

  1. Will use enumeration
  2. Familiar with aspect-oriented programming ideas
  3. Familiar with enumeration
  4. Familiar with exceptions
  5. Understand the basic knowledge of springboot
  6. Familiar with basic project construction, omit this step in the presentation
  7. springboot 2.0, jdk1.8, build tool idea

Two custom exception classes

The author can only say that the code is straightforward and concise.

2.1 Introduce relevant dependencies in the pom file

 <dependencies>
        <!-- web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
复制代码

2.2 Use enumeration to customize exception class messages

/*
 * @Author lsc
 * @Description  <p>异常消息 </p>
 * @Date 2019/10/9 20:28
 * @Param
 * @return
 **/
@AllArgsConstructor//全参构造
@NoArgsConstructor//空参构造
public enum CodeMsg {

    SUCESS(200,"sucess"),
    SERVER_ERROR(500,"服务端异常"),
    Request_Error(404,"请求异常");

    // 错误消息码
    private Integer code;
    // 错误消息提示
    private String msg;

    public Integer getCode() {
        return code;
    }

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

    public String getMsg() {
        return msg;
    }

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

复制代码

2.3 Integration of RuntimeException with custom exceptions

/**
 * @Author lsc
 * @Description <p>自定义通用异常 </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;


}
复制代码

2.4 Write page to return entity class

/**
 * @Author lsc
 * @Description <p> 返回的页面 </p>
 * @Date 2019/10/9 20:35
 * @Version 1.0
 */

@Data//set get 等方法
@AllArgsConstructor
@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){
        return new ResultPage<T>(codeMsg.getCode(),codeMsg.getMsg(),data);
    }

    //失败的时候调用
    public static <T> ResultPage<T> error(CodeMsg codeMsg){
        return new ResultPage<T>(codeMsg.getCode(),codeMsg.getMsg());
    }
}

复制代码

2.5 Exception capture

/*
 * @Author lsc
 * @Description  <p> 异常捕获</p>
 * @Date 2019/10/9 20:42
 * @Param
 * @return
 **/
@ControllerAdvice
@Configuration
public class CommonExceptionHandler {


    // 捕获CommonException异常
    @ExceptionHandler(value = CommonException.class)
    @ResponseBody
    public ResponseEntity<ResultPage> CommonExceptionHandler(CommonException e){
        // 获得异常消息
        CodeMsg codeMsg = e.getCodeMsg();
        // 设置错误消息页面返回
        return ResponseEntity.status(HttpStatus.OK).body(ResultPage.error(codeMsg));
    }

    // 下面还可以自定义捕获其他异常比如 非法参数异常 运算异常 等等.......

}

复制代码

2.6 controller layer

/**
 * @Author lsc
 * @Description <p> 控制层</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>成功消息 </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","欢迎关注博客和微信号");
        list.add(hashMap);
        return ResponseEntity.ok().body(ResultPage.sucess(CodeMsg.SUCESS,list));
    }
}
复制代码

2.7 Start class

/**
 * @Author lsc
 * @Description <p> 自定义异常启动类</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);
    }
}
复制代码

Three start the project page request test

3.1 Abnormal test

Insert picture description here

3.2 Successful test

Insert picture description here

Guess you like

Origin juejin.im/post/5e97a491e51d4546ec1cc1ec
Recommended