From 022909f01753442ea789f78bdfc041e81a5b40d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?cxxxxxxx=E8=AF=A6?= <286460656@qq.com> Date: Mon, 4 Sep 2023 16:31:08 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B5=8B=E8=AF=95spi?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- .../main/java/com/ms/api/TestSpiService.java | 42 +++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 04942f34..ccd4120a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ curl --location --request POST 'localhost:8080/base/cloud/spi' \ --data-raw '{ "appId":"7264840234423027259", "authId":"4463798", -"method":"getProductInfoByProductId", +"method":"testSpi", "data":"{\"productIds\":[682805686164],\"pageSize\":\"20\"}" }' diff --git a/ms-biz/src/main/java/com/ms/api/TestSpiService.java b/ms-biz/src/main/java/com/ms/api/TestSpiService.java index 4c1435af..521d76b0 100644 --- a/ms-biz/src/main/java/com/ms/api/TestSpiService.java +++ b/ms-biz/src/main/java/com/ms/api/TestSpiService.java @@ -1,47 +1,45 @@ package com.ms.api; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; import com.jinritemai.cloud.base.api.BaseRequest; import com.jinritemai.cloud.base.api.BaseResponse; import com.jinritemai.cloud.base.api.ExtensionService; import com.jinritemai.cloud.base.api.ExtensionServiceHandler; import com.ms.api.common.R; import com.ms.api.common.Ret; -import com.ms.api.common.SPIBaseService; import com.ms.api.dto.ItemDTO; -import com.ms.api.tool.DsJsonRequestTemplate; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.HashMap; +import java.util.Map; @ExtensionService("testSpi") @Slf4j -public class TestSpiService extends SPIBaseService implements ExtensionServiceHandler { - @Autowired - private DsJsonRequestTemplate dsJsonRequestTemplate; +public class TestSpiService implements ExtensionServiceHandler { + @Override - public BaseResponse handle(BaseRequest req) { - initHandle(req); - getAuthCode(); - String res = null; - HashMap params = new HashMap<>(); - params.put("platformItemId", 1); - params.put("sourceItemId", 33); - params.put("authCode", authCode); - try { - res = dsJsonRequestTemplate.execute("/micro_move/add_platform_item_to_source_item_relation", params); - log.info(res); - } catch (Exception e) { + public BaseResponse handle(BaseRequest req) { + + String resourceFileName = "/getProductCatTree.txt"; // 注意路径的开头是一个斜杠 + String lines = null; + try (InputStream is = InputStream.class.getResourceAsStream(resourceFileName); + BufferedReader reader = new BufferedReader(new InputStreamReader(is))) { + String line; + while ((line = reader.readLine()) != null) { + System.out.println(line); + lines = lines + line; + } + } catch (IOException e) { e.printStackTrace(); - log.error(e.getMessage()); } - return R.ok(JSON.parseObject(res)); + + Map result = new HashMap<>(); + result.put("line", lines); + + return R.ok(Ret.success(result)); } } From a49b88c3541814f6af659a1b70c9439b6473595e Mon Sep 17 00:00:00 2001 From: daixiaogang <2510234660@qq.com> Date: Mon, 4 Sep 2023 16:33:00 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=85=A8=E5=B1=80=E5=BC=82=E5=B8=B8?= =?UTF-8?q?=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ms/config/GlobalExceptionHandler.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 ms-biz/src/main/java/com/ms/config/GlobalExceptionHandler.java diff --git a/ms-biz/src/main/java/com/ms/config/GlobalExceptionHandler.java b/ms-biz/src/main/java/com/ms/config/GlobalExceptionHandler.java new file mode 100644 index 00000000..5567ca18 --- /dev/null +++ b/ms-biz/src/main/java/com/ms/config/GlobalExceptionHandler.java @@ -0,0 +1,46 @@ +package com.ms.config; + +import com.jinritemai.cloud.base.api.BaseResponse; +import com.ms.api.common.R; +import com.ms.api.common.Ret; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * 全局异常处理器 + * * @author admin + */ +@Slf4j +@ControllerAdvice +@ResponseBody +public class GlobalExceptionHandler { + + /** + * 处理自定义的业务异常 + */ + @ExceptionHandler(value = RuntimeException.class) + public BaseResponse bizExceptionHandler(RuntimeException e) { + log.error("发生业务异常! msg: -> ", e); + return R.ok(Ret.fail(e.getMessage())); + } + + /** + * 处理空指针的异常 + */ + @ExceptionHandler(value = NullPointerException.class) + public BaseResponse exceptionHandler(NullPointerException e) { + log.error("发生空指针异常! msg: -> ", e); + return R.ok(Ret.fail(e.getMessage())); + } + + /** + * 服务器异常 + */ + @ExceptionHandler(Exception.class) + public BaseResponse exception(Exception e) { + log.error("服务器异常! msg: -> ", e); + return R.ok(Ret.fail(e.getMessage())); + } +}