我想消费一个返回一堆值的休息服务。 该 bean 如下所示。
Class Customer{
Name, Address, Age ---etc // Almost 200 fields are there. Including reference to many objects as well. So it is very hard to create a bean for accepting the response.
}
是否有任何替代方法来使用响应。
客户customer = restTemplate.getForObject(http://testurl);
这不是我需要的。我需要任何其他方式来使用服务而不创建 bean。
使用Spring Boot,Java 8
最佳答案
如果您不想创建重量级 DTO,您可能想尝试在客户端获取 JSONObject。沿线的东西:
String str = restTemplate.getForObject("http://testurl", String.class);
JSONObject myCustomer = new JSONObject(str);
String name = myCustomer.getString("name");
JSONObject address = myCustomer.getJSONObject("address"); // if address is a composite object with city, street, etc...
关于java - 不使用响应 bean 的 Rest API 消费,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51607020/