如何解决使用 Spring Cloud Open Feign 获取 JSON 中的对象列表
我有一个应用程序,当我调用它的一项服务时,它返回以下 JSON:
{
"suppliers": [
{
"name": "Joaquim","email": "joaquim@email.com",},{
"name": "Manoel","email": "manoel@email.com",}
]
}
为了访问这些数据,我使用了一个具有以下方法的 Feign 客户端:
@FeignClient(name = "suppliersClient",url = "www.url-example.com.br")
// ...
List<SuppliersDTO> searchSuppliers(@RequestParam("city") String city);
根据城市,它返回供应商列表。这段代码是一个简单的例子,用来向你解释我想要做什么。
除了 name
和 email
之外,我的 DTO 供应商类还有 getters
和 setters
字段。
public class SuppliersDTO {
String name;
String email;
// GETTERS AND SETTERS
使用这样的代码,当使用 Feign Client 时,我收到 500 Internal Server Error 的信息:
"Error while extracting response for type [java.util.List<com.domain.store.dto.SuppliersDTO>] and content type [application/json;charset=utf-8]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.util.ArrayList<com.domain.store.dto.SuppliersDTO>` from Object value (token `JsonToken.START_OBJECT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<com.domain.store.dto.SuppliersDTO>` from Object value (token `JsonToken.START_OBJECT`)\n at [Source: (PushbackInputStream); line: 1,column: 1]"
从我研究和阅读的内容来看,发生错误是因为服务的 JSON 返回一个供应商对象,并且其中有一个列表。这就是 jackson 无法绑定到我的 List<SuppliersDTO>
的原因。为了解决这个问题,我创建了另一个类:
public class SuppliersListDTO {
List<SuppliersDTO> suppliers;
// GETTERS E SETTERS
现在我的 Feign 返回 SuppliersListDTO
而不是 List<SuppliersDTO>
:
@FeignClient(name = "SuppliersClient",url = "www.url-example.com.br")
// ...
SuppliersListDTO searchSuppliers(@RequestParam("city") String city);
这样就可以工作了,绑定完成,我将一个 SuppliersListDTO 返回给用户。但这似乎不是处理从 JSON 接收到的列表的最正确方法。我真的必须为此创建两个 DTO 类,其中一个只有一个变量 (List<SuppliersDTO> suppliers
) 及其 getter
和 setter
?根据您的经验,您会推荐另一种方法吗?
解决方法
响应格式无关紧要,可以是集合或不是... 按照此代码:
import lombok.Data;
import java.util.List;
@Data
public class DictionaryDto {
private final String domainDescription;
private final List<DictionaryKeyDto> keys;
}
import lombok.Data;
@Data
public class DictionaryKeyDto {
private final String keyCode;
private final String keyValue;
private final String keyExtendedValue;
}
import com.james.domain.client.DictionaryDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import javax.validation.constraints.NotEmpty;
@FeignClient(name = "${be.client.name}",url = "${be.client.url}")
public interface BeClient {
@GetMapping("dictionary/{domainCode}")
public DictionaryDto getDictionaryByDomainCode(@PathVariable @NotEmpty String domainCode);
}
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
public class StandAloneApplication {
public static void main(String[] args) {
SpringApplication.run(StandAloneApplication.class,args);
}
}
我使用了这个 mvn 依赖:
org.springframework.cloud spring-cloud-starter-openfeign版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。