当前位置:   article > 正文

SpringBoot自定义异常类

springboot自定义异常类

一、使用枚举自定义异常类消息

  1. import lombok.AllArgsConstructor;
  2. import lombok.NoArgsConstructor;
  3. /**
  4. * 异常消息
  5. */
  6. @AllArgsConstructor
  7. @NoArgsConstructor
  8. public enum CodeMsg {
  9. SUCCESS(200,"success"),
  10. SERVER_ERROR(500,"服务端异常"),
  11. Request_Error(404,"请求异常"),
  12. NOT_TOKEN(1001,"token不存在"),
  13. TIME_OUT_TOKEN(1002,"token已经过期"),
  14. VALIDA_ERROR_TOKEN(1003,"token校验失败"),
  15. DEL_ERROR_TOKEN(1004,"token删除失败");
  16. private Integer code;
  17. private String msg;
  18. public Integer getCode() {
  19. return code;
  20. }
  21. public void setCode(Integer code) {
  22. this.code = code;
  23. }
  24. public String getMsg() {
  25. return msg;
  26. }
  27. public void setMsg(String msg) {
  28. this.msg = msg;
  29. }
  30. }

二、使用自定义异常集成RuntimeException

  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. import java.lang.RuntimeException;
  5. /**
  6. * 自定义通用异常
  7. */
  8. @NoArgsConstructor
  9. @AllArgsConstructor
  10. @Data
  11. public class CommonException extends RuntimeException {
  12. private static final long serialVersionUID = 1L;
  13. //引入自定义异常消息
  14. private CodeMsg codeMsg;
  15. }

三、编写页面返回实体类

  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @Data
  5. @NoArgsConstructor
  6. @AllArgsConstructor
  7. public class ResultPage<T> {
  8. private Integer code;
  9. private String msg;
  10. private T data;
  11. ResultPage(Integer code,String msg){
  12. this.code = code;
  13. this.msg = msg;
  14. }
  15. //成功的时候调用
  16. public static <T> ResultPage<T> success(CodeMsg codeMsg,T data){
  17. return new ResultPage<T>(codeMsg.getCode(),codeMsg.getMsg(),data);
  18. }
  19. //失败的时候调用
  20. public static <T> ResultPage<T> error(CodeMsg codeMsg){
  21. return new ResultPage<>(codeMsg.getCode(),codeMsg.getMsg());
  22. }
  23. }

四、异常捕获

  1. import org.springframework.context.annotation.Configuration;
  2. import org.springframework.http.HttpStatus;
  3. import org.springframework.http.ResponseEntity;
  4. import org.springframework.web.bind.annotation.ControllerAdvice;
  5. import org.springframework.web.bind.annotation.ExceptionHandler;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. @ControllerAdvice
  8. @Configuration
  9. public class CommonExceptionHandler {
  10. //捕获CommonException异常
  11. @ExceptionHandler(value = CommonException.class)
  12. @ResponseBody
  13. public ResponseEntity<ResultPage> CommonExceptionHandler(CommonException e){
  14. //获得异常消息
  15. CodeMsg codeMsg = e.getCodeMsg();
  16. //设置错误消息页面返回
  17. return ResponseEntity.status(HttpStatus.OK).body(ResultPage.error(codeMsg));
  18. }
  19. }

五、controller层

  1. import com.smile.project.exception.utils.CodeMsg;
  2. import com.smile.project.exception.utils.CommonException;
  3. import com.smile.project.exception.utils.ResultPage;
  4. import org.springframework.http.ResponseEntity;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.ArrayList;
  8. import java.util.HashMap;
  9. @RestController
  10. public class ExceptionController {
  11. @GetMapping("/testException")
  12. public ResultPage<Object> testException(){
  13. //示范,抛出异常
  14. throw new CommonException(CodeMsg.Request_Error);
  15. }
  16. @GetMapping("testSuccess")
  17. public ResponseEntity testSuccess(){
  18. ArrayList<Object> list = new ArrayList<>();
  19. HashMap<String,Object> hashMap = new HashMap<>();
  20. hashMap.put("infos","欢迎关注博客和微信号");
  21. list.add(hashMap);
  22. return ResponseEntity.ok().body(ResultPage.success(CodeMsg.SUCCESS,list));
  23. }
  24. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码维护者/article/detail/62666
推荐阅读
相关标签
  

闽ICP备14008679号