一种模型对不同模型的响应

One model response for different models

我正在尝试 cast/map 对 APIResponse.groovy 的 API 端点响应。如果我有一个像

这样的 API 响应模型,这可以简单地完成

APIResponse.groovy

Integer age
String gender
Integer zip
Education education
String marital_status
Boolean magazine_buyer
Boolean outdoor_and_adventure

我想将它们分组为人口统计、家庭和兴趣购买。喜欢..

Demographics.groovy

Integer age
String gender
Integer zip
...

Household.groovy

Education education
Integer household_income
...

InterestPurchase.groovy

Boolean magazine_buyer
Boolean outdoor_and_adventure
...

我想到了类似的东西

APIResponse.groovy

inteface APIResponse implements Household, Demographics, InterestPurchase{

}

但是当然,这行不通,因为接口的成员是最终的和静态的..

我不确定您使用什么方法 cast/map api 回应 APIResponse.groovy。如果你能提供更多这方面的信息,也许我能以更好的方式提供帮助。

您可以尝试以下方法:

  1. Demographics.groovy、Household.groovy 和 InterestPurchase.groovy 分别映射到 api 响应,然后映射这些APIResponse.groovy.

  2. 如果不需要制作APIResponse.groovy界面,那么可以制作Demographics.groovy、Household.groovy和InterestPurchase.groovy摘要class.

  3. 如果你想使用接口方法,那么不要声明字段,只需为这些变量创建getter和setter方法他们各自的界面。