【问题标题】:ibm mobilefirst Adapter - convert JSONObject to POJO classibm mobilefirst Adapter - 将 JSONObject 转换为 POJO 类
【发布时间】:2017-03-22 09:26:21
【问题描述】:

任何人都知道 - 如何将 JSONObject 转换为 POJO 类?

我创建了一个适配器,我想在将其发送给客户端之前将其转换为 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 类 - 员工

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 静谧测试进行测试时,它返回 JsonObject 并成功返回 Body 以及来自服务的数据。

但是当我检查日志信息 ( message.log ) - 服务器日志时,状态为空。

来自服务的用户 ID:null 来自服务的状态 ID:null

似乎它的 JSON Java IBM API,它是否有像 Jackson API 这样的 ObjectMapper 来将 JsonObject 映射到 POJO 类。

  1. 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");

Results仍然为null,是否需要调用body函数“loginEmployeeResp”来指示json的restful结果,因为数据JSon Object来自服务SOAP。

【问题讨论】:

    标签: json ibm-mobilefirst mobilefirst-adapters mobilefirst-server


    【解决方案1】:

    很明显,您的String.valueOf(obj.get("userId")) 返回 null 或空,所以问题是,它的哪一部分?

    您可以登录 obj.get("userId") 并查看它是否为空,在这种情况下,响应不包含您期望的内容。

    但我怀疑问题是 String.valueOf() 转换没有达到您的预期。看起来 MobileFirst 中的 JSONObjectcom.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 对象。)

    【讨论】:

    • 嗨 dbreaux,我做了这么多类型的演员表,结果仍然是空的。我在上面编辑了我的问题,我认为它总是显示为 null,因为 json 格式 restful 与正文中没有函数名称的示例不同..
    猜你喜欢
    • 1970-01-01
    • 2020-12-03
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多