环境详情
我有三个项目:
前两个项目的第三个是依赖项。
数据流
项目B 向项目A 发出 HTTP 请求,该请求使用 Project C 转换的模型对象进行回复它是 JSON 。
项目B 使用JSONObject对 JSON 响应进行解码,并尝试使用BeanUtils获取原始POJO对象。
代码示例
ExamplePOJO类( Project C 的一部分):
public class ExamplePOJO {
private String id;
private AnotherPOJO anotherPOJO;
public void setId(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setAnotherPOJO(AnotherPOJO anotherPOJO ) {
this.anotherPOJO = anotherPOJO ;
}
public AnotherPOJO getAnotherPOJO() {
return anotherPOJO ;
}
}
项目A 示例端点:
@Path("/sample")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getExampleResponse() {
try {
ServiceResponse<ExamplePOJO> response = new ServiceResponse<ExamplePOJO>();
ExamplePOJO eo = new ExamplePOJO();
eo.setId("1");
AnotherPOJO ap = new AnotherPOJO();
eo.setAnotherPojo(ap);
response.setResponse(eo);
return ((ResponseBuilder) Response.ok().entity(response)).type("application/json").build();
} catch(Exception E) {
//error
}
}
项目A 响应对象容器:
public class ServiceResponse<T> {
private T response;
public T getResponse() {
return this.response;
}
public void setResponse(T response) {
this.response = response;
}
}
有趣的部分,项目B :
public void callService() {
//....HTTP request...
//json object
JSONObject json = new JSONObject(jsonResponse);
//ServiceResponseEquivalent is the same as the ServiceResponse object of *Project A*
decodeResponse(json, ServiceResponseEquivalent.class, ExamplePOJO.class);
}
//this is a recursive function
public <T, V> T void decodeResponse(JSONObject json, Class<?> responseModel, Class<V> responseObjectModel) {
//this is the same as the ServiceResponse object of *Project A*
Object reflectedInstance = responseModel.newInstance();
//here I got the field "response" of ServiceResponse
Field[] fields = reflectedInstance.getClass().getDeclaredFields();
for(Field field: fields) {
//I got the json object based on the field name
Object objectFromResponse = json.get(field.getName());
if(objectFromResponse instanceof JSONObject) {
//this is the "response" property of *ServiceResponse* which I know is an instance of *ExamplePOJO* class (because who calls this function pass *Class<V> responseObjectModel*
if(if(field.getName().equals("response")) {
//recursive call
objectFromResponse = decodeResponse(json.getJSONObject(field.getName()), responseObjectModel, responseObjectModel);
}
//here I found another object inside the "response" object but I don't know which class is it. In this case it's an instance of *AnotherPOJO*
else {
//I try to get the class from the name of the property: in order to work, the property must be named as its class
String className = "com.example.packace." + field.getName().toUpperCase().charAt(0) + field.getName().substring(1, field.getName().length());
//className = com.example.package.AnotherPOJO
//recursive call
objectFromResponse = decodeResponse(json.getJSONObject(field.getName()), Class.forName(className), responseObjectModel);
//I try to set the object inside the response one
//HERE IT FAILS
BeanUtils.setProperty(reflectedInstance, field.getName(), objectFromResponse);
}
//here we found another Object but we don't know
} else {
//I add the element
BeanUtils.setProperty(reflectedInstance, field.getName(), objectFromResponse);
}
}
}
JSON示例
客户端收到此 JSON :
{
"response": { //response is an instance of ExamplePOJO
"id":"1",
"anotherPOJO":{
[...]
}
},
[ ...other fields...]
}
问题
当 decodeResponse 尝试解码递归调用中的 AnotherPOJO 对象时,会抛出此异常:
java.lang.IllegalArgumentException:无法在bean类'com.example.package.ExamplePOJO'上调用com.example.package.ExamplePOJO.setAnotherPOJO - 参数类型不匹配 - 具有类型为“com.example.package.AnotherPOJO”的对象“但预期签名”com.example.package.AnotherPOJO“
从异常中可以明显看出,对象是同一个类的实例。
有什么想法吗?
这可能是解码未知类对象的更好方法?这样:
String className = "com.example.packace." + field.getName().toUpperCase().charAt(0) + field.getName().substring(1, field.getName().length());
//className = com.example.package.AnotherPOJO
//recursive call
objectFromResponse = decodeResponse(json.getJSONObject(field.getName()), Class.forName(className), responseObjectModel);
有明显的问题,必须将该字段命名为其类。
答案 0 :(得分:4)
这似乎是类加载问题。
查看Class.forName(String)
的来源,它使用来自调用者的类加载器。这可能与加载目标responseModel
的类加载器不同,因此,请尝试以下操作:
//recursive call
objectFromResponse = decodeResponse(
json.getJSONObject(field.getName()),
Class.forName(className, true, responseModel.getClassLoader()),
responseObjectModel);
这应该确保模型子层次结构类由相同的类加载器加载。
答案 1 :(得分:0)
您可以在field
变量中获得有关该课程的信息。调用field.getType()
获取当前字段/属性的类...
//here I found another object inside the "response" object. Use information from field to get class.
else {
objectFromResponse = decodeResponse(json.getJSONObject(field.getName()), field.getType(), responseObjectModel);
注意:我建议Jackson将JSON转换为java对象。