赞
踩
一、使用枚举自定义异常类消息
- import lombok.AllArgsConstructor;
- import lombok.NoArgsConstructor;
-
- /**
- * 异常消息
- */
- @AllArgsConstructor
- @NoArgsConstructor
- public enum CodeMsg {
-
- SUCCESS(200,"success"),
- SERVER_ERROR(500,"服务端异常"),
- Request_Error(404,"请求异常"),
- NOT_TOKEN(1001,"token不存在"),
- TIME_OUT_TOKEN(1002,"token已经过期"),
- VALIDA_ERROR_TOKEN(1003,"token校验失败"),
- DEL_ERROR_TOKEN(1004,"token删除失败");
-
- 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;
- }
- }
二、使用自定义异常集成RuntimeException
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import java.lang.RuntimeException;
-
- /**
- * 自定义通用异常
- */
- @NoArgsConstructor
- @AllArgsConstructor
- @Data
- public class CommonException extends RuntimeException {
-
- private static final long serialVersionUID = 1L;
-
- //引入自定义异常消息
- private CodeMsg codeMsg;
-
- }
三、编写页面返回实体类
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- public class ResultPage<T> {
-
- private Integer code;
-
- private String msg;
-
- private T data;
-
- ResultPage(Integer code,String msg){
- this.code = code;
- this.msg = msg;
- }
-
- //成功的时候调用
- public static <T> ResultPage<T> success(CodeMsg codeMsg,T data){
- return new ResultPage<T>(codeMsg.getCode(),codeMsg.getMsg(),data);
- }
-
- //失败的时候调用
- public static <T> ResultPage<T> error(CodeMsg codeMsg){
- return new ResultPage<>(codeMsg.getCode(),codeMsg.getMsg());
- }
-
- }
四、异常捕获
- import org.springframework.context.annotation.Configuration;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @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));
- }
-
- }
五、controller层
- import com.smile.project.exception.utils.CodeMsg;
- import com.smile.project.exception.utils.CommonException;
- import com.smile.project.exception.utils.ResultPage;
- import org.springframework.http.ResponseEntity;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RestController;
- import java.util.ArrayList;
- import java.util.HashMap;
-
- @RestController
- public class ExceptionController {
-
- @GetMapping("/testException")
- public ResultPage<Object> testException(){
- //示范,抛出异常
- throw new CommonException(CodeMsg.Request_Error);
- }
-
- @GetMapping("testSuccess")
- public ResponseEntity testSuccess(){
- ArrayList<Object> list = new ArrayList<>();
- HashMap<String,Object> hashMap = new HashMap<>();
- hashMap.put("infos","欢迎关注博客和微信号");
- list.add(hashMap);
- return ResponseEntity.ok().body(ResultPage.success(CodeMsg.SUCCESS,list));
- }
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。