使用 Spring Cloud Open Feign 获取 JSON 中的对象列表

Adr*_*ano 5 java spring-mvc spring-boot spring-cloud spring-cloud-feign

我有一个应用程序,当我调用其服务之一时,它会返回以下 JSON:

{
     "suppliers": [
         {
             "name": "Joaquim",
             "email": "joaquim@email.com",
         },
     {
             "name": "Manoel",
             "email": "manoel@email.com",
         }
     ]
}
Run Code Online (Sandbox Code Playgroud)

为了访问这些数据,我使用具有以下方法的 Feign 客户端:

@FeignClient(name = "suppliersClient", url = "www.url-example.com.br")
// ...
List<SuppliersDTO> searchSuppliers(@RequestParam("city") String city);
Run Code Online (Sandbox Code Playgroud)

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

除了和之外,我的 DTO Supplys 类还具有name和字段。emailgetterssetters

public class SuppliersDTO {

     String name;
     String email;

     // GETTERS AND SETTERS
Run Code Online (Sandbox Code Playgroud)

使用这样的代码,当使用 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]"
Run Code Online (Sandbox Code Playgroud)

根据我的研究和阅读,发生错误是因为服务的 JSON 返回一个供应商对象,并且其中有一个列表。这就是为什么 Jackson 无法绑定到我的List<SuppliersDTO>. 为了解决这个问题,我创建了另一个类:

public class SuppliersListDTO {
    List<SuppliersDTO> suppliers;
    // GETTERS E SETTERS
Run Code Online (Sandbox Code Playgroud)

现在我的 Feign 返回了SuppliersListDTO,但没有List<SuppliersDTO>

@FeignClient(name = "SuppliersClient", url = "www.url-example.com.br")
// ...
SuppliersListDTO searchSuppliers(@RequestParam("city") String city);
Run Code Online (Sandbox Code Playgroud)

这样它就可以工作了,绑定完成,我将一个SuppliersListDTO返回给用户。但这似乎不是处理从 JSON 接收的列表的最正确方法。我真的必须为此创建两个 DTO 类,其中一个只有一个变量 ( List<SuppliersDTO> suppliers) 及其gettersetter吗?根据您的经验,您会推荐另一种方法吗?

Tia*_*ici 2

响应格式并不重要,可以是集合也可以不是……请遵循以下代码:

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

我使用了这个 mvn 依赖项:

org.springframework.cloud spring-cloud-starter-openfeign