From 1b460b5d8be699753e913d19e2f7125bbd2bad32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=91=A8=E6=B6=9B?= Date: Thu, 31 May 2018 23:14:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=85=A8=E5=B1=80=E5=BC=82?= =?UTF-8?q?=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springboot/cloud/core/entity/Result.java | 44 ++++++++++++++----- .../core/entity/exception/BaseException.java | 35 +++++++++++++++ .../GlobalExceptionHandlerAdvice.java | 25 +++++++++++ .../src/main/resources/application.yml | 4 ++ 4 files changed, 96 insertions(+), 12 deletions(-) create mode 100644 common/core/src/main/java/com/springboot/cloud/core/entity/exception/BaseException.java create mode 100644 services/producer/src/main/java/com/springboot/producer/exception/GlobalExceptionHandlerAdvice.java diff --git a/common/core/src/main/java/com/springboot/cloud/core/entity/Result.java b/common/core/src/main/java/com/springboot/cloud/core/entity/Result.java index 82736c3..7252521 100644 --- a/common/core/src/main/java/com/springboot/cloud/core/entity/Result.java +++ b/common/core/src/main/java/com/springboot/cloud/core/entity/Result.java @@ -1,11 +1,15 @@ package com.springboot.cloud.core.entity; +import com.fasterxml.jackson.annotation.JsonIgnore; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; -import lombok.Data; +import lombok.Getter; + +import java.time.Instant; +import java.time.ZonedDateTime; @ApiModel(description = "rest请求的返回模型,所有rest正常都返回该类的对象") -@Data +@Getter public class Result { public static final String SUCCESSFUL_CODE = "000000"; @@ -17,6 +21,8 @@ public class Result { private String code; @ApiModelProperty(value = "处理结果描述信息") private String mesg; + @ApiModelProperty(value = "请求结果生成时间戳") + private Instant timestamp; @ApiModelProperty(value = "处理结果数据信息") private T data; @@ -30,6 +36,7 @@ public class Result { public Result(String code, String mesg) { this.code = code; this.mesg = mesg; + this.timestamp = ZonedDateTime.now().toInstant(); } /** @@ -38,8 +45,7 @@ public class Result { * @param data */ public Result(String code, String mesg, T data) { - this.code = code; - this.mesg = mesg; + this(code, mesg); this.data = data; } @@ -47,7 +53,7 @@ public class Result { * 快速创建成功结果并返回结果数据 * * @param data - * @return + * @return Result */ public static Result success(Object data) { return new Result(SUCCESSFUL_CODE, SUCCESSFUL_MESG, data); @@ -56,36 +62,49 @@ public class Result { /** * 快速创建成功结果 * - * @return + * @return Result */ public static Result success() { return new Result(SUCCESSFUL_CODE, SUCCESSFUL_MESG); } /** - * 快速创建失败结果没有返回数据 + * 系统异常类没有返回数据 * - * @return + * @return Result */ public static Result fail() { return new Result(ERROR_CODE, ERROR_MESG); } /** - * 快速创建失败结果并返回结果数据 + * 系统异常类并返回结果数据 * * @param data - * @return + * @return Result */ public static Result fail(Object data) { return new Result(ERROR_CODE, ERROR_MESG, data); } + /** + * 系统异常类并返回结果数据 + * + * @param code + * @param mesg + * @param data + * @return Result + */ + public static Result fail(String code, String mesg, Object data) { + return new Result(code, mesg, data); + } + /** * 成功code=000000 * - * @return + * @return true/false */ + @JsonIgnore public boolean isSuccess() { return SUCCESSFUL_CODE.equals(this.code); } @@ -93,8 +112,9 @@ public class Result { /** * 失败 * - * @return + * @return true/false */ + @JsonIgnore public boolean isFail() { return !isSuccess(); } diff --git a/common/core/src/main/java/com/springboot/cloud/core/entity/exception/BaseException.java b/common/core/src/main/java/com/springboot/cloud/core/entity/exception/BaseException.java new file mode 100644 index 0000000..321772f --- /dev/null +++ b/common/core/src/main/java/com/springboot/cloud/core/entity/exception/BaseException.java @@ -0,0 +1,35 @@ +package com.springboot.cloud.core.entity.exception; + +import lombok.Getter; + +/** + * Created by zhoutaoo on 2018/5/31. + */ +@Getter +public class BaseException extends RuntimeException { + + private String code; + private String mesg; + + public BaseException(String code, String mesg, String message, Throwable cause) { + super(message, cause); + this.code = code; + this.mesg = mesg; + } + + public BaseException(String code, String mesg, String message) { + super(message); + this.code = code; + this.mesg = mesg; + } + + public BaseException(String code, String mesg) { + this(code); + this.mesg = mesg; + } + + public BaseException(String code) { + super(); + this.code = code; + } +} diff --git a/services/producer/src/main/java/com/springboot/producer/exception/GlobalExceptionHandlerAdvice.java b/services/producer/src/main/java/com/springboot/producer/exception/GlobalExceptionHandlerAdvice.java new file mode 100644 index 0000000..bbb8a09 --- /dev/null +++ b/services/producer/src/main/java/com/springboot/producer/exception/GlobalExceptionHandlerAdvice.java @@ -0,0 +1,25 @@ +package com.springboot.producer.exception; + +import com.springboot.cloud.core.entity.Result; +import com.springboot.cloud.core.entity.exception.BaseException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalExceptionHandlerAdvice { + + @ExceptionHandler(value = {BaseException.class}) + public Result serviceException(BaseException ex) { + return Result.fail(ex.getCode(), ex.getMesg(), ex.getMessage()); + } + + @ExceptionHandler(value = {Exception.class}) + public Result exception() { + return Result.fail(); + } + + @ExceptionHandler(value = {Throwable.class}) + public Result throwable() { + return Result.fail(); + } +} \ No newline at end of file diff --git a/services/producer/src/main/resources/application.yml b/services/producer/src/main/resources/application.yml index ba1f56e..f1bba2b 100644 --- a/services/producer/src/main/resources/application.yml +++ b/services/producer/src/main/resources/application.yml @@ -26,6 +26,10 @@ spring: url: http://${BOOT_ADMIN_HOST:localhost}:${BOOT_ADMIN_PORT:8022} zipkin: baseUrl: http://localhost:8091 + mvc: + throw-exception-if-no-handler-found: true + resources: + add-mappings: false logging: level: -- Gitee