我正在尝试让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"
}
这是我试图反序列化的相应类:
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;
}
}
这是我的 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);
}
我需要做什么才能使其正常工作?