Hystrix将List视为不兼容的返回类型

时间:2018-09-13 06:40:46

标签: hystrix

我有以下Hystrix后备广告

public List<Data> getDataFallback(String id, LocalDate startDate, LocalDate endDate, Throwable t) {
    log.warn("Could not get data");
    throw new DataException(ErrorMessage.builder()
            .code(COULD_NOT_GET_DATA.name())
            .internalMessage(t.getMessage())
            .build());
}

它被附加到以下方法

@Override
@HystrixCommand(fallbackMethod = "getDataFallback")
public List<Data> getData(String id, LocalDate startDate, LocalDate endDate) {
    return repository.find(id, startDate, endDate);
}

在运行JUnit测试时,大概在运行服务时,在运行getData方法时也会收到以下错误消息

com.netflix.hystrix.contrib.javanica.exception.FallbackDefinitionException: Incompatible return types. 
Command method: public java.util.List package.class.getData(java.lang.String,java.time.LocalDate,java.time.LocalDate);
Fallback method: public java.util.List package.class.getDataFallback(java.lang.String,java.time.LocalDate,java.time.LocalDate,java.lang.Throwable);
Hint: Different size of types variables.
Command  type literals size = 2: [java.util.List<package.Data>, class package.Data]
Fallback type literals size = 1: [interface java.util.List]

如果我将列表包装在包装对象中,则完全相同的配置可以顺利进行。有什么作用?

1 个答案:

答案 0 :(得分:0)

方法返回类型 和参数应该相同 仅一个额外的参数(Throwable throwable)

@GetMapping("/msg")
@HystrixCommand(fallbackMethod="fallBackGetUsers")
public String localMessage() {
    return userServices.localMassage();

}

/*
 * fallBack method parameter should be same and One Extra parameter will be
 * there only  and Return type also same


 */
public String  fallBackGetUsers(Throwable throwable) {
    return "fallback";
}