Caused by: java.lang.IllegalStateException: Only single-level inheritance supported: XxxxService

gaojie1024發表於2020-11-07

Spring Cloud專案啟動中,報如下異常:

Caused by: java.lang.IllegalStateException: Only single-level inheritance supported: UserClientService
	at feign.Util.checkState(Util.java:129)
	at feign.Contract$BaseContract.parseAndValidateMetadata(Contract.java:51)
	at feign.ReflectiveFeign$ParseHandlersByName.apply(ReflectiveFeign.java:151)
	at feign.ReflectiveFeign.newInstance(ReflectiveFeign.java:49)
	at feign.Feign$Builder.target(Feign.java:269)
	at org.springframework.cloud.openfeign.HystrixTargeter.target(HystrixTargeter.java:38)
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.loadBalance(FeignClientFactoryBean.java:301)
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getTarget(FeignClientFactoryBean.java:330)
	at org.springframework.cloud.openfeign.FeignClientFactoryBean.getObject(FeignClientFactoryBean.java:310)
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:171)
	... 63 common frames omitted
Disconnected from the target VM, address: '127.0.0.1:57393', transport: 'socket'

Process finished with exit code 1

點選日誌Contract.java原因分析,檢視報錯程式碼:

feign\Contract.java

public List<MethodMetadata> parseAndValidateMetadata(Class<?> targetType) {
      checkState(targetType.getTypeParameters().length == 0, "Parameterized types unsupported: %s",
          targetType.getSimpleName());
      checkState(targetType.getInterfaces().length <= 1, "Only single inheritance supported: %s",
          targetType.getSimpleName());
      if (targetType.getInterfaces().length == 1) {
        checkState(targetType.getInterfaces()[0].getInterfaces().length == 0,
            "Only single-level inheritance supported: %s",
            targetType.getSimpleName());
      }
      final Map<String, MethodMetadata> result = new LinkedHashMap<String, MethodMetadata>();
      for (final Method method : targetType.getMethods()) {
        if (method.getDeclaringClass() == Object.class ||
            (method.getModifiers() & Modifier.STATIC) != 0 ||
            Util.isDefault(method)) {
          continue;
        }
        final MethodMetadata metadata = parseAndValidateMetadata(targetType, method);
        checkState(!result.containsKey(metadata.configKey()), "Overrides unsupported: %s",
            metadata.configKey());
        result.put(metadata.configKey(), metadata);
      }
      return new ArrayList<>(result.values());
    }

分析其原因是Service介面只能繼承級別只能一級;

解決方法:

去掉Service介面父級的繼承即可。

相關文章