简体   繁体   English

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

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

I want to insert data to another database with Feign, and i have error我想用 Feign 将数据插入另一个数据库,但我有错误

This Configuration此配置

@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());
        }
    };
}

This Service Feign Client这个服务假装客户端

@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);
}

And i want to call this endpoint to my Controller我想将此端点称为我的 Controller

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

And when i run this application i have error当我运行此应用程序时出现错误

Unsatisfied dependency expressed through constructor parameter 0;通过构造函数参数 0 表示的不满足的依赖关系; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.beepay.service.internal.BeeShopServiceClient': Unexpected exception during bean creation;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名称为“com.beepay.service.internal.BeeShopServiceClient”的 bean 时出错:创建 bean 期间出现意外异常; nested exception is java.lang.NoClassDefFoundError: feign/Capability嵌套异常是 java.lang.NoClassDefFoundError: feign/Capability

How I resolve this?我如何解决这个问题? sorry my bad englist对不起我的坏英语

You do not need the @Configuration in the configuration class for feign:您不需要配置 class 中的 @Configuration 用于 feign:

Configuration does not need to be annotated with @Configuration.配置不需要使用@Configuration 注解。 However, if it is, then take care to exclude it from any @ComponentScan that would otherwise include this configuration as it will become the default source for feign.Decoder, feign.Encoder, feign.Contract, etc., when specified.但是,如果是,请注意将其从任何可能包含此配置的 @ComponentScan 中排除,因为在指定时它将成为 feign.Decoder、feign.Encoder、feign.Contract 等的默认源。 This can be avoided by putting it in a separate, non-overlapping package from any @ComponentScan or @SpringBootApplication, or it can be explicitly excluded in @ComponentScan.这可以通过将其从任何@ComponentScan 或@SpringBootApplication 放在一个单独的、不重叠的package 中来避免,或者可以在@ComponentScan 中明确排除它。

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

Also make sure to enable feign via: @EnableFeignClients in you main config class还要确保通过主配置 class 中的 @EnableFeignClients 启用 feign

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

相关问题 如何配置Feign客户端以在没有Consul的情况下工作 - How to configure Feign client to work without Consul 如何在 spring 引导 Java 中动态获取假客户端名称和 url - how to get feign client name and url both dynamically in spring boot Java 我如何获得Feign客户的名字? - How can I get name of the Feign client? Java Spring - 拦截 REST 客户端的执行 (Hystrix Feign) - Java Spring - intercepting execution of a REST Client (Hystrix Feign) 如何从Spring Boot Feign客户端登录到远程Web服务 - How can I login to remote webservice from spring boot feign client Spring RestAPI with Feign Client and Pageable - Spring RestAPI with Feign Client and Pageable 如何从spring-cloud-netflix-feign在Feign-Client中设置HostnameVerifier - How to set HostnameVerifier in Feign-Client from spring-cloud-netflix-feign 如何使用 postman 或 feign 客户端在 Spring 引导中将值设置为 @RequestAttribute - How to set value to @RequestAttribute in Spring boot using postman or feign client 如何使用 Spring Boot feign 客户端进行 Oauth2 身份验证? - How to use Spring Boot feign client for Oauth2 authentication? 如何在Spring Boot应用程序中的Feign客户端上使用WireMock? - How to use WireMock on a Feign client in a Spring Boot application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM