Spring Feign:无法提取响应:找不到适合响应类型的 HttpMessageConverter

uri*_*rig 5 spring spring-cloud-feign feign

我正在尝试让Spring Cloud Netflix Feign客户端通过 HTTP 获取一些 JSON 并将其转换为对象。我不断收到此错误:

org.springframework.web.client.RestClientException:无法提取响应:没有找到适合响应类型 [class io.urig.checkout.Book] 和内容类型 [application/json;charset=UTF-8] 的 HttpMessageConverter

以下是从远程服务返回的 JSON 内容:

{
    "id": 1,
    "title": "Moby Dick",
    "author": "Herman Melville"
}
Run Code Online (Sandbox Code Playgroud)

这是我尝试反序列化的相应类:

package io.urig.checkout;

public class Book {
    private long id;
    private String title;
    private String author;

    public Book() {}

    public Book(long id, String title, String author) {
        super();
        this.id = id;
        this.title = title;
        this.author = author;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的 Feign 客户端:

package io.urig.checkout;

import java.util.Optional;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import io.urig.checkout.Book;

@FeignClient(name="inventory", url="http://localhost:8080/")
public interface InventoryClient {

    @RequestMapping(method = RequestMethod.GET, value = "books/{bookId}")
    public Optional<Book> getBookById(@PathVariable(value="bookId") Long bookId);

}
Run Code Online (Sandbox Code Playgroud)

我需要做什么才能让它发挥作用?

uri*_*rig 0

感谢所有试图提供帮助的人!

事实证明,我的问题是 Maven 依赖项有缺陷,可能在下载或安装过程中损坏。完全删除了.m2/repository我机器上的文件夹,然后更新了项目的 Maven 依赖项,问题现在就消失了。