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 82736c3cb4bc21b135dfda4e622e3a3269ddbfd6..7252521d8369db1c0d6567e35fed742bc506c9cf 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 0000000000000000000000000000000000000000..321772fcf188b1263c3d36b02295b6338caadd2a --- /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 0000000000000000000000000000000000000000..bbb8a0907c5024020ef6e8ae73e84c1e5fdaa4c5 --- /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 ba1f56e004f0cebdcc575790624997a51c60c553..f1bba2bb6a398cb6d5ed1d1c182adfffd08a01fa 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: