使用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);

根据城市的不同,它会返回供应商列表。这个代码是一个简单的例子,可以向你解释我正在尝试做什么

除了getterssetters之外,我的DTO供应商类还有nameemail字段。

public class SuppliersDTO {
String name;
String email;
// GETTERS AND SETTERS

有了这样的代码,当使用Feign客户端时,我得到了一个500内部服务器错误,信息是:

"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(及其gettersetter?根据你的经验,你会推荐另一种方法吗?

响应格式无关紧要,可以是Collection也可以是。。。遵循以下代码:

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.cloudspring-cloud启动器openforeign

最新更新