ibm mobilefirst 适配器 - 将 JSONObject 转换为 POJO class
ibm mobilefirst Adapter - convert JSONObject to POJO class
任何人都知道 - 如何将 JSON对象转换为 POJO class?
我创建了一个适配器,我想在将它发送给客户端之前将其转换为 Pojo。
1) 我的 ResourceAdapterResource.java(适配器)
@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;
}
2.) Pojo Class - 员工
import java.io.Serializable;
@SuppressWarnings("serial")
public class Employee implements Serializable{
private String userid;
private String userStatus;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid= userid;
}
public String getUserStatus() {
return userStatus;
}
public void setUserStaus(String userStatus) {
this.userStatus= userStatus;
}
}
当我使用 Swagger - MobileFirst Console restful 进行测试时,它 return JsonObject 成功地 return 包含服务数据的主体。
但是当我检查日志信息 (message.log) - 服务器日志时,状态为空。
来自服务的用户 ID:空
来自服务的状态 ID:null
似乎是它的 JSON Java IBM API ,它有像 Jackson API 这样的 ObjectMapper 来将 JsonObject 映射到 POJO Class.
- 来自 Swagger 的结果
{
"statusReason": "OK",
"responseHeaders": {
"Content-Length": "1849",
"Content-Language": "en-US",
"Date": "Thu, 23 Mar 2017 01:40:33 GMT",
"X-Powered-By": "Servlet/3.0",
"Content-Type": "text/xml; charset=utf-8"
},
"isSuccessful": true,
"responseTime": 28,
"totalTime": 33,
"warnings": [],
"Envelope": {
"soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
"Body": {
"checkEmployeeLoginResponse": {
"a": "http://com.fndong.my/employee_Login/",
"loginEmployeeResp": {
"Employee": {
"idmpuUserName": "fndong",
"Status": "A",
"userid": "fndong",
"Password": "AohIeNooBHfedOVvjcYpJANgPQ1qq73WKhHvch0VQtg@=",
"PwdCount": "1",
"rEmail": "fndong@gmail.com"
},
"sessionId": "%3F",
"statusCode": "0"
}
}
}
},
"errors": [],
"info": [],
"statusCode": 200
}
然后我按照你的建议转换为字符串:
String objUserId = (String) objectAuth.get("userid");
结果还是null,是否需要调用body函数"loginEmployeeResp"来表示jsonrestful结果,因为数据JSon对象来自service香皂。
显然您的 String.valueOf(obj.get("userId"))
返回 null 或空,所以问题是,它的哪一部分?
您可以记录 obj.get("userId")
并查看它是否为空,在这种情况下响应不包含您期望的内容。
但我怀疑问题是 String.valueOf()
转换没有按照您的预期进行。看起来 MobileFirst 中的 JSONObject
是 com.ibm.json.java.JSONObject
,当我搜索 that 时,example I found 只是转换为 String
:
emp.setUserId((String) obj.get("userId"));
编辑: 既然您已经添加了 Swagger 结果,我想您的 obj.get("userId")
可能会返回 null 本身。你检查了吗?
一方面,"userId" 不是 "userid"。大小写很重要。
但更重要的是,"userid" 嵌套在 JSON 的深处,所以我认为仅从顶层 JSONObject
获取它是行不通的。我认为您必须执行以下操作:
JSONObject envelope = (JSONObject) obj.get("Envelope");
JSONObject body = (JSONObject) envelope.get("Body");
JSONObject response = (JSONObject) body.get("checkEmployeeLoginResponse");
JSONObject resp = (JSONObject) response.get("loginEmployeeResp");
JSONObject employee = (JSONObject) resp.get("Employee");
emp.setUserId((String) employee.get("userid"));
emp.setUserStatus((String) employee.get("status"));
(遗憾的是,对于特定的 IBM JSON4J,我不认为有一种方法可以更自动地将 JSON 解组为 Java 对象。)
任何人都知道 - 如何将 JSON对象转换为 POJO class?
我创建了一个适配器,我想在将它发送给客户端之前将其转换为 Pojo。
1) 我的 ResourceAdapterResource.java(适配器)
@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;
}
2.) Pojo Class - 员工
import java.io.Serializable;
@SuppressWarnings("serial")
public class Employee implements Serializable{
private String userid;
private String userStatus;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid= userid;
}
public String getUserStatus() {
return userStatus;
}
public void setUserStaus(String userStatus) {
this.userStatus= userStatus;
}
}
当我使用 Swagger - MobileFirst Console restful 进行测试时,它 return JsonObject 成功地 return 包含服务数据的主体。
但是当我检查日志信息 (message.log) - 服务器日志时,状态为空。
来自服务的用户 ID:空 来自服务的状态 ID:null
似乎是它的 JSON Java IBM API ,它有像 Jackson API 这样的 ObjectMapper 来将 JsonObject 映射到 POJO Class.
- 来自 Swagger 的结果
{
"statusReason": "OK",
"responseHeaders": {
"Content-Length": "1849",
"Content-Language": "en-US",
"Date": "Thu, 23 Mar 2017 01:40:33 GMT",
"X-Powered-By": "Servlet/3.0",
"Content-Type": "text/xml; charset=utf-8"
},
"isSuccessful": true,
"responseTime": 28,
"totalTime": 33,
"warnings": [],
"Envelope": {
"soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
"Body": {
"checkEmployeeLoginResponse": {
"a": "http://com.fndong.my/employee_Login/",
"loginEmployeeResp": {
"Employee": {
"idmpuUserName": "fndong",
"Status": "A",
"userid": "fndong",
"Password": "AohIeNooBHfedOVvjcYpJANgPQ1qq73WKhHvch0VQtg@=",
"PwdCount": "1",
"rEmail": "fndong@gmail.com"
},
"sessionId": "%3F",
"statusCode": "0"
}
}
}
},
"errors": [],
"info": [],
"statusCode": 200
}
然后我按照你的建议转换为字符串:
String objUserId = (String) objectAuth.get("userid");
结果还是null,是否需要调用body函数"loginEmployeeResp"来表示jsonrestful结果,因为数据JSon对象来自service香皂。
显然您的 String.valueOf(obj.get("userId"))
返回 null 或空,所以问题是,它的哪一部分?
您可以记录 obj.get("userId")
并查看它是否为空,在这种情况下响应不包含您期望的内容。
但我怀疑问题是 String.valueOf()
转换没有按照您的预期进行。看起来 MobileFirst 中的 JSONObject
是 com.ibm.json.java.JSONObject
,当我搜索 that 时,example I found 只是转换为 String
:
emp.setUserId((String) obj.get("userId"));
编辑: 既然您已经添加了 Swagger 结果,我想您的 obj.get("userId")
可能会返回 null 本身。你检查了吗?
一方面,"userId" 不是 "userid"。大小写很重要。
但更重要的是,"userid" 嵌套在 JSON 的深处,所以我认为仅从顶层 JSONObject
获取它是行不通的。我认为您必须执行以下操作:
JSONObject envelope = (JSONObject) obj.get("Envelope");
JSONObject body = (JSONObject) envelope.get("Body");
JSONObject response = (JSONObject) body.get("checkEmployeeLoginResponse");
JSONObject resp = (JSONObject) response.get("loginEmployeeResp");
JSONObject employee = (JSONObject) resp.get("Employee");
emp.setUserId((String) employee.get("userid"));
emp.setUserStatus((String) employee.get("status"));
(遗憾的是,对于特定的 IBM JSON4J,我不认为有一种方法可以更自动地将 JSON 解组为 Java 对象。)