如何将Feign Clients对象链接到特殊响应键

时间:2019-01-30 13:11:25

标签: java spring spring-boot spring-cloud-feign

我正在创建一个REST API,以在Spring Boot上使用Feign Client调用另一个API。生成的JSON响应与我的模型不完全相同。这是我得到的:

{
  "success": true,
  "data": [
    {
      "id": 1,
      "name": "DC-01",
      "site": "10.168.3.11",
      "created_at": "2018-12-27T06:28:21.098134+00:00",
      "modified_at": "2019-01-14T03:48:57.109484+00:00"
    }
  ],
}

我的模型是这样的:

public class Providers {
    private Integer id;
    private String name;
    private String site;
    private Date created_at;
    private Date modified_at;
}

我的伪装客户端界面如下:

@FeignClient(name = "CerberusClient", url = "${service.cerberus.url}")
public interface CerberusClient {

    @RequestMapping(value = "/providers/", method = RequestMethod.GET, produces = "application/json")
    List<Providers> getAllProviders();
}

我的休息控制器是这样的:

@RestController
@RequestMapping("/cerberus/")
public class CerberusProvidersImpl {

    @Autowired
    private CerberusClient cerberusClient;

    @RequestMapping(value = "/getAllProviders", method = RequestMethod.GET, produces = "application/json")
    public List<Providers> getAllProviders() {
        return cerberusClient.getAllProviders();
    }
}

但是当我调用程序的Rest API时,它会产生如下结果:

status": 500,
"error": "Internal Server Error",
"message": "Error while extracting response for type[java.util.List<com.infrastructure.nemesis.feign.model.cerberus.Providers>] and content type [application/json]; 
nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON 
parse error: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token; nested exception is 
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of START_OBJECT token\n at [Source: (PushbackInputStream); line: 1, column: 1]",

我的FeignClient中必须有一些东西可以链接模型并仅读取“数据”值,因此它可以适合模型。在这种情况下我应该使用什么方法?

1 个答案:

答案 0 :(得分:0)

请尝试包括以下更改

@FeignClient(name = "CerberusClient", url = "${service.cerberus.url}")
public interface CerberusClient {

    @RequestMapping(value = "/providers/", method = RequestMethod.GET, produces = "application/json")
    List < Object > getAllProviders();

    @RequestMapping(value = "/providers/", method = RequestMethod.GET, produces = "application/json")
    Object getAllProviders();    
}

其中一个应该可以工作