0

I am creating a REST API to call another API using Feign Client on Spring Boot. The JSON response that is produced isn't exact same with my Models. Here's what I got:

{
  "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"
    }
  ],
}

My model is like this:

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

My feign client interface is like this:

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

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

My rest controller is like this:

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

But when I call the Rest API of my program, it produces result like this:

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]",

There must be something in my FeignClient to link the model and just read the "data" value, so It can fit the model. What Method should I use for this case ?

Williams Perdana
  • 169
  • 1
  • 3
  • 16

1 Answers1

0

Please try including the following changes

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

One of these should work