作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想得到一个 Spring Cloud Netflix Feign客户端通过 HTTP 获取一些 JSON 并将其转换为对象。我不断收到此错误:
org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class io.urig.checkout.Book] and content type [application/json;charset=UTF-8]
{
"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;
}
}
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);
}
最佳答案
我不知道 Feign,但是当我过去出现“找不到合适的 HttpMessageConverter...”错误时,这是因为尚未注册内容类型。也许您需要将其添加到 RequestMapping 中:
consumes = "application/json"
GitHub github = Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.target(GitHub.class, "https://api.github.com");
关于 Spring 假装 : Could not extract response: no suitable HttpMessageConverter found for response type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49195867/
我是一名优秀的程序员,十分优秀!