Browse Source

处理poems feign异常

lixing 4 years ago
parent
commit
b9b585a577

+ 41 - 0
apm-common/src/main/java/com/persagy/apm/common/config/PoemsFeignExceptionHandler.java

@@ -0,0 +1,41 @@
+package com.persagy.apm.common.config;
+
+import com.persagy.apm.common.exception.RpcException;
+import com.persagy.apm.common.response.FeignResponseContent;
+import com.persagy.apm.common.response.FeignResponseItem;
+import com.persagy.apm.common.response.FeignResponseMsg;
+import com.persagy.apm.common.response.PoemsFeignResponse;
+import com.persagy.common.enums.ResponseCode;
+import org.aspectj.lang.annotation.AfterReturning;
+import org.aspectj.lang.annotation.Aspect;
+import org.aspectj.lang.annotation.Pointcut;
+
+/**
+ * feign调用poems接口,如果接口返回了报错信息,将报错信息抛出
+ *
+ * @author lixing
+ * @version V1.0 2021/3/24 4:55 下午
+ **/
+@Aspect
+public class PoemsFeignExceptionHandler {
+
+    @Pointcut("@within(org.springframework.cloud.openfeign.FeignClient)")
+    private void pointCutMethod() {
+    }
+
+    @AfterReturning(pointcut = "pointCutMethod()", returning = "res")
+    public void process(Object res) {
+        String result = null;
+        String reason = null;
+        if (res instanceof PoemsFeignResponse) {
+            PoemsFeignResponse response = (PoemsFeignResponse) res;
+            result = response.getResult();
+            reason = response.getReason();
+        }
+
+        String success = "success";
+        if (!success.equals(result)) {
+            throw new RpcException(reason);
+        }
+    }
+}

+ 25 - 0
apm-common/src/main/java/com/persagy/apm/common/response/PoemsFeignResponse.java

@@ -0,0 +1,25 @@
+package com.persagy.apm.common.response;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+import org.apache.poi.ss.formula.functions.T;
+
+import java.util.List;
+
+/**
+ * poems feign的返回结果
+ *
+ * @author lixing
+ * @version V1.0 2021/5/19 2:41 下午
+ **/
+@ApiModel
+@Data
+public class PoemsFeignResponse {
+    @ApiModelProperty("是否调用成功。failure失败,success成功")
+    private String result;
+    @ApiModelProperty("调用失败原因")
+    private String reason;
+    @ApiModelProperty("返回的结果对象")
+    private List content;
+}