杰克逊情结波乔

Jackson Complex Pojo

我一直在努力使这个 JSON 成为一个 POJO,以便我可以 marshal/unmarshal 随意使用它,但是我很难找到一种方法来完成这一部分:

{
  "jsonrpc": "2.0",
  "method": "deploy",
  "params": {
    "type": 1,
    "chaincodeID": {
      "path": "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
    },
    "ctorMsg": {
      "args": [
        "init",
        "a",
        "1000",
        "b",
        "2000"
      ]
    },
    "secureContext": "lukas"
  },
  "id": 1
}

如您所见,有一个“params”属性,其中有 2 个对象,其中一个是 JSON 数组。

public class JsonPayload {
    private String jsonrpc;
    private String method;
    private ??? params;
    private int id;

    public JsonPayload(String jsonrpc, String method, ??? params, int id) {
        this.jsonrpc = jsonrpc;
        this.method = method;
        this.params = params;
        this.id = id;
    }

    public String getJsonrpc() {
        return jsonrpc;
    }

    public void setJsonrpc(String jsonrpc) {
        this.jsonrpc = jsonrpc;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public ??? getParams() {
        return params;
    }

    public void setParams(??? params) {
        this.params = params;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

我的问题是如何在 POJO 中对参数进行建模。我想我可以再创建 2 个对象并将它们传递给它,但这使它比我希望的简单 JSON 封送处理/解封处理要复杂得多。

有简单的方法吗?有或没有杰克逊?

这就是 Param POJO 的样子,如果您想对其建模(不包括 getter 和 setter):

class Params {
    Integer type;
    @JsonProperty("chaincodeID")
    ChainCode chainCode;
    @JsonProperty("ctorMsg")
    private Message message;
    String secureContext;
}

class ChainCode{
    String path;
}

class Message {
    List<String> args;
}

如果您根本不打算使用 params 或者您不关心类型,那么您可以使用 @JsonIgnoreProperties 作为基础 class 或定义 params 作为 Map<String, Object>