From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-core-cloud/src/main/java/org/springblade/core/cloud/feign/BladeFeignFallback.java | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 102 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-core-cloud/src/main/java/org/springblade/core/cloud/feign/BladeFeignFallback.java b/Source/BladeX-Tool/blade-core-cloud/src/main/java/org/springblade/core/cloud/feign/BladeFeignFallback.java new file mode 100644 index 0000000..0b7e306 --- /dev/null +++ b/Source/BladeX-Tool/blade-core-cloud/src/main/java/org/springblade/core/cloud/feign/BladeFeignFallback.java @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2018-2028, DreamLu All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: DreamLu 鍗㈡槬姊� (596392912@qq.com) + */ +package org.springblade.core.cloud.feign; + +import com.fasterxml.jackson.databind.JsonNode; +import feign.FeignException; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.api.ResultCode; +import org.springblade.core.tool.jackson.JsonUtil; +import org.springblade.core.tool.utils.ObjectUtil; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; +import org.springframework.lang.Nullable; + +import java.lang.reflect.Method; +import java.util.*; + +/** + * blade fallBack 浠g悊澶勭悊 + * + * @author L.cm + */ +@Slf4j +@AllArgsConstructor +public class BladeFeignFallback<T> implements MethodInterceptor { + private final Class<T> targetType; + private final String targetName; + private final Throwable cause; + private final static String CODE = "code"; + + @Nullable + @Override + public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { + String errorMessage = cause.getMessage(); + log.error("BladeFeignFallback:[{}.{}] serviceId:[{}] message:[{}]", targetType.getName(), method.getName(), targetName, errorMessage); + Class<?> returnType = method.getReturnType(); + // 闆嗗悎绫诲瀷鍙嶉绌洪泦鍚� + if (List.class == returnType || Collection.class == returnType) { + return Collections.emptyList(); + } + if (Set.class == returnType) { + return Collections.emptySet(); + } + if (Map.class == returnType) { + return Collections.emptyMap(); + } + // 鏆傛椂涓嶆敮鎸� flux锛宺x锛屽紓姝ョ瓑锛岃繑鍥炲�间笉鏄� R锛岀洿鎺ヨ繑鍥� null銆� + if (R.class != returnType) { + return null; + } + // 闈� FeignException + if (!(cause instanceof FeignException)) { + return R.fail(ResultCode.INTERNAL_SERVER_ERROR, errorMessage); + } + FeignException exception = (FeignException) cause; + byte[] content = exception.content(); + // 濡傛灉杩斿洖鐨勬暟鎹负绌� + if (ObjectUtil.isEmpty(content)) { + return R.fail(ResultCode.INTERNAL_SERVER_ERROR, errorMessage); + } + // 杞崲鎴� jsonNode 璇诲彇锛屽洜涓虹洿鎺ヨ浆鎹紝鍙兘 瀵规柟鏀惧洖鐨勫苟 涓嶆槸 R 鐨勬牸寮忋�� + JsonNode resultNode = JsonUtil.readTree(content); + // 鍒ゆ柇鏄惁 R 鏍煎紡 杩斿洖浣� + if (resultNode.has(CODE)) { + return JsonUtil.getInstance().convertValue(resultNode, R.class); + } + return R.fail(resultNode.toString()); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + BladeFeignFallback<?> that = (BladeFeignFallback<?>) o; + return targetType.equals(that.targetType); + } + + @Override + public int hashCode() { + return Objects.hash(targetType); + } +} -- Gitblit v1.9.3