java - 使用 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);
根据城市,它会返回供应商列表。这段代码是一个简单的例子,向你解释我想要做什么。
除了and之外,我的 DTO Suppliers 类还有name
andemail
字段。getters
setters
public class SuppliersDTO {
String name;
String email;
// GETTERS AND SETTERS
使用这样的代码,当使用 Feign 客户端时,我得到一个 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 返回一个供应商对象,并且其中有一个列表。这就是为什么杰克逊不能绑定到我的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
andsetter
吗?根据您的经验,您会推荐另一种方法吗?
解决方案
响应格式无关紧要,可以是集合还是不...遵循此代码:
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推荐阅读
- excel - 在 Excel 表格中查找包含参数 x 但不包含参数 y 的单元格
- excel - 从 Excel 单元格 (VBA) 调用的打印数组
- shopware - 如何将 shopware6 中创建的订单的订单状态从开放更改为报价?
- python-3.x - pdftotext ImportError:DLL 加载失败:访问被拒绝
- amazon-web-services - 使用 AWS 客户端 VPN 终端节点的 AWS Transit Gateway 跨账户 VPC 访问
- c# - C# 从文档中删除 svg 节点
- telegram-bot - 在用户机器人聊天中创建消息链接
- java - 如何验证使用 base64UUID 生成的 UUID
- spring-webflux - 如何在 WebFilter 中更改 API responseBody 消息 - Spring Flux
- javascript - ReplaceAll 用于大逗号分隔的字符串