我打一個RESTful的第三方API,它總是發送JSON的格式如下:配置XStream的動態映射到不同的對象
{
"response": {
...
}
}
哪裏...
是響應對象需要被映射回一個Java POJO。例如,有時JSON將包含應該被映射回Fruit
POJO數據:
{
"response": {
"type": "orange",
"shape": "round"
}
}
...有時JSON將包含應被映射回一個Employee
POJO數據:
{
"response": {
"name": "John Smith",
"employee_ID": "12345",
"isSupervisor": "true",
"jobTitle": "Chief Burninator"
}
}
所以取決於REST的API調用,我們需要這兩個JSON結果映射回兩者之一:
public class Fruit {
private String type;
private String shape;
// Getters & setters for all properties
}
public class Employee {
private String name;
private Integer employeeId;
private Boolean isSupervisor;
private String jobTitle;
// Getters & setters for all properties
}
不幸的是,我不能昌e此第三方REST服務總是發回一個{ "response": { ... } }
JSON結果。但我仍然需要一種方式來配置映射到動態映射這樣的response
回無論是Fruit
或Employee
。
首先,我想Jackson了有限的成功,但我想這是它不是作爲配置。所以現在我試圖用XStream及其JettisonMappedXmlDriver
將JSON映射回POJO。這裏的原型代碼我有:
public static void main(String[] args) {
XStream xs = new XStream(new JettisonMappedXmlDriver());
xs.alias("response", Fruit.class);
xs.alias("response", Employee.class);
// When XStream sees "employee_ID" in the JSON, replace it with
// "employeeID" to match the field on the POJO.
xs.aliasField("employeeID", Employee.class, "employee_ID");
// Hits 3rd party RESTful API and returns the "*fruit version*" of the JSON.
String json = externalService.getFruit();
Fruit fruit = (Fruit)xs.fromXML(json);
}
不幸的是,當我運行此我得到一個例外,因爲我有xs.alias("response", ...)
映射response
到2個不同的Java對象:
Caused by: com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$UnknownFieldException: No such field me.myorg.myapp.domain.Employee.type
---- Debugging information ----
field : type
class : me.myorg.myapp.domain.Employee
required-type : me.myorg.myapp.domain.Employee
converter-type : com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path : /response/type
line number : -1
version : null
-------------------------------
於是我問:什麼都可以我是否會繞過這樣的事實,即API將始終發送相同的「包裝器」response
JSON對象?我能想到的唯一的事情就是首先做一個像這樣的字符串替換:
String json = externalService.getFruit();
json = json.replaceAll("response", "fruit");
...
但是,這似乎是一個醜陋的黑客。 XStream(或其他映射框架)是否提供了可以幫助我解決這個特定情況的任何事情? Thansk提前。