feign.codec.EncodeException: class ArrayList/HashMap is not a type supported by this encoder

Storm中使用Fegin,因为Storm是通过拓扑自己创建和管理Bolt的,所以在Bolt中使用到了某个FeignClient就无法通过@Service,然后通过@Autowired获取到FeginClient的实例。

使用下面的代码来实例化FeginClient,

    /**
     * 初始化SanService的FeginClient
     * @param url
     * @return
     */
    public static SanService initializeSanServiceFegin(String url) {
        SanService service = Feign.builder()
                .contract(new SpringMvcContract())
                .target(SanService.class, url);

        return service;
    }

但会报下面的错误异常:

feign.codec.EncodeException: class is not a type supported by this encoder

因为默认的Encoder不支持List或Map,所以报错。

如果自定义Encoder,则必须要用到Feign 依赖包中其他的类,而且需要通过@Autowired依赖注入其他类,但上面提到了,Storm中无法通过@Autowired依赖注入。

终极解决办法:

/**
 * 初始化SanService的FeginClient
 * @param url
 * @return
 */
public static SanService initializeSanServiceFegin(String url) {
    HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(new ObjectMapper());
    ObjectFactory converter = ()-> new HttpMessageConverters(jsonConverter);
    SanService service = Feign.builder()
            .encoder(new SpringEncoder(converter))
            .decoder(new SpringDecoder(converter))
            .contract(new SpringMvcContract())
            .target(SanService.class, url);

    return service;
}

 

你可能感兴趣的:(feign.codec.EncodeException: class ArrayList/HashMap is not a type supported by this encoder)