繁体   English   中英

如何在 java spring 中配置 Feign 客户端

[英]How to i configure Feign Client in java spring

我想用 Feign 将数据插入另一个数据库,但我有错误

此配置

@Configuration
public class ServiceFeignClientConfig {
public ErrorDecoder errorDecoderBeeShop(Decoder decoder) {
    return (String methodKey, Response response) -> {
        try {
            ResponseShop respEnvelope = (ResponseShop) decoder.decode(response, ResponseShop.class);
            MarshallingUtils.printJson(respEnvelope);
            ErrCode errCode = ErrCode.toError(respEnvelope.getMeta().getCode());
            String errMessage = respEnvelope.getMeta().getMessage();
            return new Exception(errCode, errMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return new Exception(ErrCode.ERR_UNKNOWN, ErrCode.ERR_UNKNOWN.getMessage());
        }
    };
}

这个服务假装客户端

@FeignClient(name = "beeshop-service", url = "http://localhost:9008/", configuration = {ServiceFeignClientConfig.class})
public interface BeeShopServiceClient {

@PutMapping("v1/input-user")
void inputUser(String merchantName, String phoneNumber, String email, Role.ERole roleMerchant);
}

我想将此端点称为我的 Controller

beeShopService.updateOrderStatus(merchantName, phoneNumber, email, roleMerchant);

当我运行此应用程序时出现错误

通过构造函数参数 0 表示的不满足的依赖关系; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“com.beepay.service.internal.BeeShopServiceClient”的 bean 时出错:创建 bean 期间出现意外异常; 嵌套异常是 java.lang.NoClassDefFoundError: feign/Capability

我如何解决这个问题? 对不起我的坏英语

您不需要配置 class 中的 @Configuration 用于 feign:

配置不需要使用@Configuration 注解。 但是,如果是,请注意将其从任何可能包含此配置的 @ComponentScan 中排除,因为在指定时它将成为 feign.Decoder、feign.Encoder、feign.Contract 等的默认源。 这可以通过将其从任何@ComponentScan 或@SpringBootApplication 放在一个单独的、不重叠的package 中来避免,或者可以在@ComponentScan 中明确排除它。

参考: https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html

还要确保通过主配置 class 中的 @EnableFeignClients 启用 feign

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM