gpt4 book ai didi

Spring 假装 : Could not extract response: no suitable HttpMessageConverter found for response type

转载 作者:行者123 更新时间:2023-12-04 03:01:05 25 4
gpt4 key购买 nike

我想得到一个 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]



这是从远程服务返回的 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);

}

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

最佳答案

我不知道 Feign,但是当我过去出现“找不到合适的 HttpMessageConverter...”错误时,这是​​因为尚未注册内容类型。也许您需要将其添加到 RequestMapping 中:

consumes = "application/json"

我只能建议尝试确认 Feign 配置是否将 MappingJackson2HttpMessageConverter 注册为 Book 的转换器。不确定这是否应该与 Feign 一起开箱即用,或者您是否必须手动完成。我在 Feign 的 GitHub 上看到一个例子,它有:
GitHub github = Feign.builder()
.encoder(new JacksonEncoder())
.decoder(new JacksonDecoder())
.target(GitHub.class, "https://api.github.com");

您是否使用 Feign.builder() 或一些等效的配置文件创建了配置?

关于 Spring 假装 : Could not extract response: no suitable HttpMessageConverter found for response type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49195867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com