ibm mobilefirst适配器-将JSONObject转换为POJO类
有人知道如何将JSONObject转换为POJO类吗 我已经创建了一个适配器,我想在将其发送到客户端之前将其转换为Pojo 1 my ResourceAdapterResource.java适配器ibm mobilefirst适配器-将JSONObject转换为POJO类,json,ibm-mobilefirst,mobilefirst-adapters,mobilefirst-server,Json,Ibm Mobilefirst,Mobilefirst Adapters,Mobilefirst Server,有人知道如何将JSONObject转换为POJO类吗 我已经创建了一个适配器,我想在将其发送到客户端之前将其转换为Pojo 1 my ResourceAdapterResource.java适配器 @POST @Path("profiles/{userid}/{password}") @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public JSONObject getStatus(@PathParam
@POST
@Path("profiles/{userid}/{password}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public JSONObject getStatus(@PathParam("userid") String userid, @PathParam("password") String password) throws IOException {
Map<String, Object> maps = new HashMap<String,Object>();
map.put("userid", userid);
map.put("password",password);
// json Object will get the value from SOAP Adapter Service
JSONObject obj = soapAdapterService(maps);
/** Question here, how to add to POJO.. I have code here but not work, null values**/
// set to Object Pojo Employee
Employee emp = new Employee();
emp.setUserId(String.valueOf(obj.get("userId")));
emp.setUserStatus((String.valueOf(obj.get("userStatus")));
// when I logging its show Empty.
logger.info("User ID from service : " + emp.getUserId());
logger.info("Status Id from service : " + emp.getUserStatus());
return obj;
}
当我使用Swagger-MobileFirst Console restful测试时,它返回JsonObject,并成功返回带有服务数据的Body
但是当我检查log info message.log-服务器日志时,状态为null
来自服务的用户ID:null
来自服务的状态Id:null
似乎它的JSON Java IBM API,是否有类似Jackson API的ObjectMapper来将JsonObject映射到POJO类
招摇过市
{
原因:好的,
负责人:{
内容长度:1849,
内容语言:en US,
日期:2017年3月23日星期四格林尼治标准时间01:40:33,
X-Powered-By:Servlet/3.0,
内容类型:text/xml;字符集=utf-8
},
isSuccessful:正确,
答复时间:28,,
总时间:33,
警告:[],
信封:{
soapenv:http://schemas.xmlsoap.org/soap/envelope/,
正文:{
checkEmployeeLoginResponse:{
a:http://com.fndong.my/employee_Login/,
loginEmployeeResp:{
雇员:{
idmpuUserName:fndong,
现状:A,
userid:fndong,
密码:aohienobhbfedovvjcypjangpqq73wkhvch0vqtg@,
PwdCount:1,
重播:fndong@gmail.com
},
会话ID:%3F,
状态代码:0
}
}
}
},
错误:[],
信息:[],
状态代码:200
}
然后我按照你的建议投了一票:
String objUserId = (String) objectAuth.get("userid");
结果仍然为null,是否需要通过调用主体函数loginEmployeeResp来指示json restful结果,因为数据json对象来自服务SOAP。显然,您的String.valueOfobj.getuserId返回null或空,所以问题是,它的哪一部分
您可以记录obj.getuserId并查看它是否为空,在这种情况下,响应不包含您期望的内容
但我怀疑问题在于转换的String.valueofconversion没有达到预期效果。MobileFirst中的JSONObject看起来像是com.ibm.json.java.JSONObject,当我搜索它时,简单地转换为字符串:
String objUserId = (String) objectAuth.get("userid");
emp.setUserIdString obj.getuserId;
编辑:现在您已经添加了招摇过市的结果,我想说您的obj.getuserId本身可能正在返回null。你查过了吗
首先,userId不是userId。资本化很重要
但更重要的是,userid嵌套在JSON的深处,所以我认为仅仅从顶级JSONObject获取它是行不通的。我想你必须做一些类似的事情:
JSONObject信封=JSONObject obj.getEnvelope;
JSONObject body=JSONObject envelope.getBody;
JSONObject response=JSONObject body.getcheckEmployeeLoginResponse;
JSONObject resp=JSONObject response.getloginEmployeeResp;
JSONObject employee=JSONObject resp.getEmployee;
emp.setUserIdString employee.getuserid;
emp.setUserStatusString employee.getstatus;
遗憾的是,对于那个特定的IBMJSON4J,我认为没有办法将JSON更自动地解组到Java对象中。Hi-dbreaux,我已经做了很多类型的转换,结果仍然是空的。我已经编辑了上面的问题,我认为它总是显示null,因为json格式restful与示例不同,在主体中没有函数名。。