【问题标题】:Using spring cloud feign causes java.lang.NoClassDefFoundError: feign/Logger使用 spring cloud feign 导致 java.lang.NoClassDefFoundError: feign/Logger
【发布时间】:2015-08-24 14:47:01
【问题描述】:

我为这样的 feignClients 启用了 Spring Cloud:

@Configuration
@EnableAutoConfiguration
@RestController
@EnableEurekaClient
@EnableCircuitBreaker
@EnableFeignClients

public class SpringCloudConfigClientApplication {
}

但是当我添加 enableFeignClients 后,我在编译时遇到了这个错误,

java.lang.NoClassDefFoundError: feign/Logger
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
    at java.lang.Class.getDeclaredMethods(Class.java:1962)

我的 POM 是

<parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <version>1.0.0.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>demo.SpringCloudConfigClientApplication</start-class>
        <java.version>1.7</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>

    </dependencies>

为了解决 feign logger 问题,我还需要向 POM 添加什么其他依赖项?

谢谢

谢谢@spencergibb,根据你的建议,在我更换了我的 pom.xml 后它就起作用了。现在我有另一个使用 FeignClient 的问题。请看下面:

@Autowired
    StoreClient storeClient;
    @RequestMapping("/stores")
    public List<Store> stores() {
        return storeClient.getStores();
    }

界面是:

@FeignClient("store")
public interface StoreClient {
    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Store> getStores();
}

店铺实体是:

public class Store {

    private long id;
    private String name;
    private String zip;

    public Store(long id, String name, String zip) {
        this.id = id;
        this.name = name;
        this.zip = zip;
    }
}

现在当我在 URL 中检索时,我得到了这个错误,

ue Jun 09 15:30:10 PDT 2015
There was an unexpected error (type=Internal Server Error, status=500).
Could not read JSON: No suitable constructor found for type [simple type, class demo.entity.Store]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.PushbackInputStream@7db6c3dc; line: 1, column: 3] (through reference chain: java.util.ArrayList[0]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class demo.entity.Store]: can not instantiate from JSON object (need to add/enable type information?) at [Source: java.io.PushbackInputStream@7db6c3dc; line: 1, column: 3] (through reference chain: java.util.ArrayList[0])

这里的错误似乎是检索到的列表无法转换为存储类。 那么为了使用 FeignClient,我们还需要包含任何其他映射器来将 JSON 转换为对象吗?

谢谢

【问题讨论】:

  • 回答我自己,必须在实体类中放置一个默认的空构造函数才能使映射器工作。谢谢

标签: spring-cloud netflix-feign


【解决方案1】:

你不见了spring-cloud-starter-feign

【讨论】:

  • 谢谢你,@spencergibb,你是对的。你能帮忙告诉如何从 JSON 映射到对象吗? FeignClient 似乎没有这样做。
  • 你的对象是否有一个公共的无参数构造函数?
猜你喜欢
  • 1970-01-01
  • 2015-06-08
  • 2017-07-23
  • 2018-06-14
  • 2015-10-21
  • 2018-08-31
  • 2022-01-16
  • 2018-04-01
  • 2015-10-12
相关资源
最近更新 更多