diff --git a/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ResultHelper.java b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ResultHelper.java index f04408c334d5009e55fa1ba461ca8ca73a7dab3c..569fb46c744456cace263eafafdfa40eaf85a381 100644 --- a/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ResultHelper.java +++ b/whatsmars-common/src/main/java/org/hongxi/whatsmars/common/result/ResultHelper.java @@ -31,4 +31,8 @@ public class ResultHelper { } } + public static Result newResult(int code, String message) { + return new Result(code, message); + } + } diff --git a/whatsmars-mq/whatsmars-mq-kafka/src/main/resources/application.yml b/whatsmars-mq/whatsmars-mq-kafka/src/main/resources/application.yml index e690fb49fb166406a26030664a0525713a5fb636..b4914594e8dd24ff53c28af35e573ec8853bab3e 100644 --- a/whatsmars-mq/whatsmars-mq-kafka/src/main/resources/application.yml +++ b/whatsmars-mq/whatsmars-mq-kafka/src/main/resources/application.yml @@ -1,6 +1,9 @@ spring: kafka: bootstrap-servers: 127.0.0.1:9092 + producer: + value-serializer: consumer: group-id: testGroup - auto-offset-reset: earliest \ No newline at end of file + auto-offset-reset: earliest + value-deserializer: \ No newline at end of file diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/controller/SampleController.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/controller/SampleController.java index 9d72c2f265aa9f85ea00b42af26ab3f93795c8e7..a998f2dccd67a99d793b2387ca194d7edff8a351 100644 --- a/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/controller/SampleController.java +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/controller/SampleController.java @@ -1,6 +1,7 @@ package org.hongxi.whatsmars.boot.sample.web.controller; -import org.hongxi.whatsmars.boot.sample.web.model.JsonResponse; +import org.hongxi.whatsmars.common.result.Result; +import org.hongxi.whatsmars.common.result.ResultHelper; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -14,9 +15,9 @@ import org.springframework.web.bind.annotation.RestController; public class SampleController { @GetMapping("/hello") - public JsonResponse hello(@RequestParam String userId, - @RequestParam Integer age) { + public Result hello(@RequestParam String userId, + @RequestParam Integer age) { String hello = userId + "," + age; - return JsonResponse.success(hello); + return ResultHelper.newSuccessResult(hello); } } diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/exception/DefaultExceptionHandler.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/exception/DefaultExceptionHandler.java index da0a366f42558640d76ac4b8658a55fb8cc6afbb..df436e5d640a4595f0ae5692e9cd7eb3c842aa19 100644 --- a/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/exception/DefaultExceptionHandler.java +++ b/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/exception/DefaultExceptionHandler.java @@ -1,7 +1,8 @@ package org.hongxi.whatsmars.boot.sample.web.exception; import lombok.extern.slf4j.Slf4j; -import org.hongxi.whatsmars.boot.sample.web.model.JsonResponse; +import org.hongxi.whatsmars.common.result.Result; +import org.hongxi.whatsmars.common.result.ResultHelper; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ControllerAdvice; @@ -21,19 +22,19 @@ public class DefaultExceptionHandler { @ResponseStatus(HttpStatus.OK) @ExceptionHandler(BusinessException.class) @ResponseBody - public JsonResponse handleLogicException(HttpServletRequest request, BusinessException e) { + public Result handleLogicException(HttpServletRequest request, BusinessException e) { log.error("business exception handled, request:{}", request.getRequestURI(), e); - return JsonResponse.error(e.getCode(), e.getMsg()); + return ResultHelper.newResult(e.getCode(), e.getMsg()); } @ResponseStatus(HttpStatus.OK) @ExceptionHandler(Exception.class) @ResponseBody - public JsonResponse handleException(HttpServletRequest request, Exception e) throws Exception { + public Result handleException(HttpServletRequest request, Exception e) throws Exception { if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) { throw e; } log.error("exception handled, request:{}", request.getRequestURI(), e); - return JsonResponse.error(500, e.getMessage()); + return ResultHelper.newResult(500, e.getMessage()); } } diff --git a/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/model/JsonResponse.java b/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/model/JsonResponse.java deleted file mode 100644 index 10a6495ef48bc434565bcd80fa27ddf514b0a5a6..0000000000000000000000000000000000000000 --- a/whatsmars-spring-boot-samples/whatsmars-boot-sample-web/src/main/java/org/hongxi/whatsmars/boot/sample/web/model/JsonResponse.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.hongxi.whatsmars.boot.sample.web.model; - -/** - * Created by shenhongxi on 2020/8/16. - */ -public class JsonResponse { - - public static final int SUCCESS_CODE = 0; - - // 错误码,0表示成功 - private int code; - // 错误原因 - private String msg; - // 服务器下发数据时间 - private long millisecond; - - private T data; - - public int getCode() { - return code; - } - - public void setCode(int code) { - this.code = code; - } - - public String getMsg() { - return msg; - } - - public void setMsg(String msg) { - this.msg = msg; - } - - public long getMillisecond() { - return millisecond; - } - - public void setMillisecond(long millisecond) { - this.millisecond = millisecond; - } - - public T getData() { - return data; - } - - public void setData(T data) { - this.data = data; - } - - public static JsonResponse success(T value) { - JsonResponse response = new JsonResponse<>(); - response.setCode(SUCCESS_CODE); - response.setMsg("ok"); - response.setMillisecond(System.currentTimeMillis()); - response.setData(value); - return response; - } - - public static JsonResponse error(int ec, String em) { - JsonResponse response = new JsonResponse<>(); - response.setCode(ec); - response.setMsg(em); - response.setMillisecond(System.currentTimeMillis()); - return response; - } -}