由于开头的字符无效,Feign 客户端无法将响应 json 转换为 Java 对象

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

我创建了一个 Feign 客户端 EmployeeServiceClient.java 如下所示

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
}

服务 https://internel.omnesys.org/v1/employees(这是由不同团队管理的内网 REST 服务)给我的响应寿命如下所示

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

我遇到假装异常,因为服务响应在开头

中包含一个额外的 )}]',

我已经要求服务团队删除那些无效字符,但他们说无法删除它,因为它是为了其他一些要求而特意放置的,并要求我从我们这边处理它。

谁能帮我解决这个问题

我看到三个选项:

  1. 使用 custom configuration and provide your own Decoder which will handle the crazy response ;) Extend ResponseEntityDecoder 自定义您的客户端并添加您的特殊响应处理。

  2. 将方法签名改为returnfeign.Response自行处理:

@FeignClient(name = "employeeclient", url = "https://internel.omnesys.org")
public interface EmployeeServiceClient {
  @RequestMapping(method = RequestMethod.GET, value = "/v1/employees")
  feign.Response getEmployeeDetails();
}
  1. 类似于第二个选项:将您的方法签名更改为 return 字符串。清理生成的字符串后,您将能够通过 jackson 等将 json 映射到您的 类

请注意:对于 2. 和 3.,根本不会有任何错误处理,您应该注意这一点

如果不选择第一个选项来隐藏解析和异常处理并确保当前方法签名,也可以考虑添加适配器。