Reputation: 4886
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
Upvotes: 2
Views: 22770
Reputation: 103
This method saved me from tears, I added it to the config class of the Feignclient :
@Bean public Encoder feignEncoder(){
HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());
ObjectFactory objectFactory = () -> new HttpMessageConverters(jacksonConverter);
return new SpringEncoder(objectFactory); }
hope it will help u to.
Upvotes: 0
Reputation: 3667
I see three options:
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.
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(); }
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.
Upvotes: 7