Search code examples
javaspring-cloudnetflix-feignspring-cloud-feignfeign

Feign Client not able to convert the response json to Java Object because of invalid character in the beginning


I have created a Feign client EmployeeServiceClient.java like as shown below

EmployeeServiceClient.java

@FeignClient(name = "employeeclient", url = "https://internel.omnesys.org")
public interface EmployeeServiceClient {
    @RequestMapping(method = RequestMethod.GET, value = "/v1/employees")
    List<EmployeeDetails> getEmployeeDetails();
}

EmployeeDetails.java

public class EmployeeDetails {
  private Employee employee;
  private String empId;
  // getters and setters
}

Employee.java

public class Employee {
  private String name;
  private String firstName;
  private String lastName;
  private String city;
  // getters and setters
}

The service https://internel.omnesys.org/v1/employees (this is a intranet REST service managed by a different team) gives me a response life as shown below

)}]',
[{"employee":{"name":"Emp1","firstName":"firstName1","lastName":"lastName1","city":"city1"},"empId":"empId123"},{"employee":{"name":"Emp2","firstName":"firstName2","lastName":"lastName2","city":"city2"},"empId":"empId456"}]

I am getting feign exception because the service response contains an additional )}]', in the starting

I have asked the service team to remove those invalid characters but they said it is not possible to remove it since it was been placed purposely for some other requirement and have asked me to handle it from our end.

Can anyone please help me on this


Solution

  • I see three options:

    1. Customize your client with custom configuration and provide your own Decoder which will handle the crazy response ;) Extend ResponseEntityDecoder and add your special response treatment.

    2. Change the method signature to return feign.Response and handle it by yourself:

    @FeignClient(name = "employeeclient", url = "https://internel.omnesys.org")
    public interface EmployeeServiceClient {
      @RequestMapping(method = RequestMethod.GET, value = "/v1/employees")
      feign.Response getEmployeeDetails();
    }
    
    1. Similar to the second option: Change your method signature to return String. After cleaning the resulting String you'll be able to map the json to your classes by jackson etc.

    Please note: for 2. and 3., there will be no error handling at all and you should take care of this

    Also consider adding a adapter if not choosing the first option to hide the parsing and exception handling and ensuring the current method signature.