diff --git a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/controller/TestController.java b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/controller/TestController.java index 4e52f77..46a4e7a 100644 --- a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/controller/TestController.java +++ b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/controller/TestController.java @@ -1,6 +1,7 @@ package club.mydlq.exceptionhandler.controller; import club.mydlq.exceptionhandler.entity.ResponseInfo; +import club.mydlq.exceptionhandler.enums.ResultEnum; import club.mydlq.exceptionhandler.exception.MyException; import club.mydlq.exceptionhandler.exception.NotFountResourceException; import org.springframework.web.bind.annotation.GetMapping; @@ -14,9 +15,8 @@ public class TestController { */ @GetMapping("/normal") public ResponseInfo normal() { - ResponseInfo responseInfo = new ResponseInfo(); - responseInfo.success("正常"); - return responseInfo; + String data = "模拟的响应数据"; + return new ResponseInfo(ResultEnum.SUCCESS,data); } /** diff --git a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/entity/ResponseInfo.java b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/entity/ResponseInfo.java index 9387632..747f2ea 100644 --- a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/entity/ResponseInfo.java +++ b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/entity/ResponseInfo.java @@ -1,36 +1,66 @@ package club.mydlq.exceptionhandler.entity; -import lombok.AllArgsConstructor; +import club.mydlq.exceptionhandler.enums.ResultEnum; import lombok.Data; -import lombok.NoArgsConstructor; @Data -@NoArgsConstructor -@AllArgsConstructor public class ResponseInfo { // 错误码 private Integer code; // 错误信息 private String message = ""; // 返回结果 - private Object result = ""; + private Object data = ""; + + public ResponseInfo() { + } + + /** + * 设置 code 与 message 构造方法 + * @param code 返回码 + * @param message 返回消息 + */ + public ResponseInfo(Integer code, String message) { + this.code = code; + this.message = message; + } + + /** + * 接收枚举信息的构造方法 + * @param resultEnum 返回枚举对象 + */ + public ResponseInfo(ResultEnum resultEnum) { + this.code = ResultEnum.SUCCESS.getCode(); + this.message = ResultEnum.SUCCESS.getMessage(); + } + + /** + * 接收枚举信息,且能接收数据的方法 + * @param resultEnum 返回枚举对象 + * @param data 返回数据 + */ + public ResponseInfo(ResultEnum resultEnum, Object data) { + this.code = ResultEnum.SUCCESS.getCode(); + this.message = ResultEnum.SUCCESS.getMessage(); + this.data = data; + } /** * 返回成功信息 * @param result 返回的结果信息,一般指获取的资源 */ - public void success(Object result) { + public void setSuccess(Object result) { this.code = ResultEnum.SUCCESS.getCode(); this.message = ResultEnum.SUCCESS.getMessage(); - this.result = result; + this.data = result; } /** * 返回错误信息 - * @param code 错误码 + * @param code 错误码 * @param message 错误消息 */ - public void error(Integer code,String message){ + public void setError(Integer code, String message) { this.code = code; this.message = message; } @@ -39,7 +69,7 @@ public void error(Integer code,String message){ * 返回错误信息 * @param exceptionEnum 错误 Enum 枚举 */ - public void error(ResultEnum exceptionEnum){ + public void setError(ResultEnum exceptionEnum) { this.code = exceptionEnum.getCode(); this.message = exceptionEnum.getMessage(); } diff --git a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/entity/ResultEnum.java b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/enums/ResultEnum.java similarity index 85% rename from springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/entity/ResultEnum.java rename to springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/enums/ResultEnum.java index e434a36..c214ae9 100644 --- a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/entity/ResultEnum.java +++ b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/enums/ResultEnum.java @@ -1,8 +1,10 @@ -package club.mydlq.exceptionhandler.entity; +package club.mydlq.exceptionhandler.enums; +/** + * 定义返回码的枚举类 + */ public enum ResultEnum { - // 据操作错误定义 SUCCESS(1000, "Success"), UNKNOWN_ERROR(1001,"未知的错误!"), NOT_FOUNT_RESOURCE(1002,"没有找到相关资源!"), diff --git a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/handler/ErrorHandler.java b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/handler/ErrorHandler.java index f7ad8bc..9bd6509 100644 --- a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/handler/ErrorHandler.java +++ b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/handler/ErrorHandler.java @@ -1,13 +1,12 @@ package club.mydlq.exceptionhandler.handler; import club.mydlq.exceptionhandler.entity.ResponseInfo; -import club.mydlq.exceptionhandler.entity.ResultEnum; +import club.mydlq.exceptionhandler.enums.ResultEnum; import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; - import javax.servlet.http.HttpServletRequest; /** @@ -23,15 +22,12 @@ public String getErrorPath() { @GetMapping("/error") @ResponseStatus(code = HttpStatus.NOT_FOUND) - public ResponseInfo handleError(HttpServletRequest request){ + public ResponseInfo handleError(HttpServletRequest request) { Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code"); - ResponseInfo responseInfo = new ResponseInfo(); - if (statusCode == 404){ - responseInfo.error(ResultEnum.NOT_FOUNT_RESOURCE); - }else{ - responseInfo.error(ResultEnum.UNKNOWN_ERROR); + if (statusCode == 404) { + return new ResponseInfo(ResultEnum.NOT_FOUNT_RESOURCE); } - return responseInfo; + return new ResponseInfo(ResultEnum.UNKNOWN_ERROR); } } diff --git a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/handler/GlobalExceptionHandler.java b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/handler/GlobalExceptionHandler.java index 385abec..4150ce5 100644 --- a/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/handler/GlobalExceptionHandler.java +++ b/springboot-exception-handler/src/main/java/club/mydlq/exceptionhandler/handler/GlobalExceptionHandler.java @@ -1,6 +1,6 @@ package club.mydlq.exceptionhandler.handler; -import club.mydlq.exceptionhandler.entity.ResultEnum; +import club.mydlq.exceptionhandler.enums.ResultEnum; import club.mydlq.exceptionhandler.entity.ResponseInfo; import club.mydlq.exceptionhandler.exception.MyException; import club.mydlq.exceptionhandler.exception.NotFountResourceException; @@ -17,53 +17,47 @@ public class GlobalExceptionHandler { /** * MyException异常处理器 - * @param e - * @return + * + * @param e MyException + * @return ResponseInfo */ @ExceptionHandler(MyException.class) @ResponseStatus(code = HttpStatus.BAD_REQUEST) - public ResponseInfo myExceptionHandler(MyException e){ - ResponseInfo responseInfo = new ResponseInfo(); - // 将枚举类中的异常信息设置到responseInfo对象中 - responseInfo.error(ResultEnum.PARAMETER_ERROR); + public ResponseInfo myExceptionHandler(MyException e) { // 判断异常消息是否为空,如果抛出异常时在异常中设定了异常消息, // 则用优先使用异常中设定的信息替换枚举类中设定的异常信息 - if (StringUtils.isEmpty(e.getMessage())){ - responseInfo.setMessage(e.getMessage()); + if (!StringUtils.isEmpty(e.getMessage())) { + return new ResponseInfo(ResultEnum.PARAMETER_ERROR.getCode(), e.getMessage()); } - return responseInfo; + return new ResponseInfo(ResultEnum.PARAMETER_ERROR); } /** * NotFountResourceException异常处理器 - * @param e - * @return + * @param e NotFountResourceException + * @return ResponseInfo */ @ExceptionHandler(NotFountResourceException.class) @ResponseStatus(code = HttpStatus.NOT_FOUND) - public ResponseInfo nodFountResourceExceptionHandler(NotFountResourceException e){ - ResponseInfo responseInfo = new ResponseInfo(); - responseInfo.error(ResultEnum.NOT_FOUNT_RESOURCE); - if (StringUtils.isEmpty(e.getMessage())){ - responseInfo.setMessage(e.getMessage()); + public ResponseInfo nodFountResourceExceptionHandler(NotFountResourceException e) { + if (StringUtils.isEmpty(e.getMessage())) { + return new ResponseInfo(ResultEnum.NOT_FOUNT_RESOURCE.getCode(), e.getMessage()); } - return responseInfo; + return new ResponseInfo(ResultEnum.NOT_FOUNT_RESOURCE); } /** * 全局异常处理器 - * @param e - * @return + * @param e Exception + * @return ResponseInfo */ @ExceptionHandler(Exception.class) @ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR) - public ResponseInfo globleExceptionHandler(Exception e){ - ResponseInfo responseInfo = new ResponseInfo(); - responseInfo.error(ResultEnum.UNKNOWN_ERROR); - if (StringUtils.isEmpty(e.getMessage())){ - responseInfo.setMessage(e.getMessage()); + public ResponseInfo globleExceptionHandler(Exception e) { + if (StringUtils.isEmpty(e.getMessage())) { + return new ResponseInfo(ResultEnum.UNKNOWN_ERROR.getCode(), e.getMessage()); } - return responseInfo; + return new ResponseInfo(ResultEnum.UNKNOWN_ERROR); } } \ No newline at end of file