2

API 呼び出しから返された結果を逆シリアル化しようとしています。ただし、結果にはブール値または配列のいずれかを含めることができます。

結果がブール値の場合、応答で受信した JSON コンテンツは次のようになります。

{
  "succeeded": true,
  "version": 1.0
}

結果が配列の場合、応答で受け取る JSON は次のようになります。

{
  "succeeded": {
  "current_page": 1,
  "per_page": 100,
  "results": [
    {
      "get_info": {
        "fieldA": "4198126",
        "fieldB": "2016-05-25T22:43:52Z",
        "fieldC": "iws-user-cfg-proxy-beta",
        "updated_at": "2016-05-25T22:43:52Z"
      }
    },
    {
      "get_info": {
        "fieldA": "4551542",
        "fieldB": "2016-07-27T22:26:27Z",
        "fieldC": "silkRoot",
        "updated_at": "2016-07-27T22:26:27Z"
      }
    }
  ]
},
"version": 1.0
}

「成功」フィールドに関連付けられた値を読みたいと思います。マッピングクラスでこれを処理する方法はありますか?

私の現在のマッピングクラスは次のとおりです。

 public class ServResp {

public final static String TYPE1_EXCEPTION = "Type1Exception";
public final static String TYPE2_EXCEPTION = "Type2Exception";

public final int httpStatusCode;
public final boolan succeeded;
public final String version;
public final String exception;
public final String exceptionMessage;

private ServResp(Builder builder) {
    this.httpStatusCode = builder.httpStatusCode;
    this.succeeded = builder.succeeded;
    this.version = builder.version;
    this.exception = builder.exception;
    this.exceptionMessage = builder.exceptionMessage;
}

public Builder modify() {
    return new Builder(this);
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((exception == null) ? 0 : exception.hashCode());
    result = prime * result + ((exceptionMessage == null) ? 0 : exceptionMessage.hashCode());
    result = prime * result + httpStatusCode;
    result = prime * result + (succeeded ? 17 : 19);
    result = prime * result + ((version == null) ? 0 : version.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    ServResp other = (ServResp) obj;
    if (exception == null) {
        if (other.exception != null)
            return false;
    } else if (!exception.equals(other.exception))
        return false;
    if (exceptionMessage == null) {
        if (other.exceptionMessage != null)
            return false;
    } else if (!exceptionMessage.equals(other.exceptionMessage))
        return false;
    if (httpStatusCode != other.httpStatusCode)
        return false;
    if (succeeded != other.succeeded)
        return false;
    if (version == null) {
        if (other.version != null)
            return false;
    } else if (!version.equals(other.version))
        return false;

    return true;
}

public static class Builder {

    private int httpStatusCode;
    private boolean succeeded;
    private String version;
    private String exception;
    private String exceptionMessage;

    public Builder() {
    }

    public Builder(ServResp other) {
        this.httpStatusCode = other.httpStatusCode;
        this.version = other.version;
        this.exception = other.exception;
        this.exceptionMessage = other.exceptionMessage;
    }

    public Builder setHttpStatusCode(int httpStatusCode) {
        this.httpStatusCode = httpStatusCode;
        return this;
    }

    public Builder setSucceeded(boolean succeeded) {
        this.succeeded = succeeded;
        return this;
    }

    public Builder setVersion(String version) {
        this.version = version;
        return this;
    }

    public Builder setException(String exception) {
        this.exception = exception;
        return this;
    }

    public Builder setExceptionMessage(String exceptionMessage) {
        this.exceptionMessage = exceptionMessage;
        return this;
    }

    public ServResp build() {
        return new ServResp(this);
    }
}}

プログラムをそのまま実行すると、次のエラーが発生します。

原因: org.codehaus.jackson.map.JsonMappingException: START_OBJECT トークンから java.lang.boolean のインスタンスを逆シリアル化できません

これを回避する方法はありますか?

4

2 に答える 2

1

の型を に変更してBuilder.succeededからObject、コードを追加して後で読み取ることができます。これは将来のバグの原因のように思えますが、API を制御できない場合は、これが最善の策かもしれません。

public class Foo {
    private Object overRiddenJsonType;

    public Object getOverRiddenJsonType() {
        return overRiddenJsonType;
    }

    public void setOverRiddenJsonType(Object overRiddenJsonType) {
        this.overRiddenJsonType = overRiddenJsonType;
    }
}

public class FooConsumer {
    public void consumeFoo(Foo foo) {
        Boolean b = false;
        Bar bar = null;
        if (foo.getOverRiddenJsonType() instanceof Boolean) {
            b = (Boolean)foo.getOverRiddenJsonType();
            // Worry about an NPE from unboxing later...
        } else if (foo.getOverRiddenJsonType() instanceof Bar) {
            bar = (Bar)foo.getOverRiddenJsonType();
        }
        // ...
    }
}

一方、API を制御する場合、より良い解決策は、JSON をsuccess常にに再構築booleanし、残りのデータを最上位フィールドまたは のメンバーにすることですresults

{
  "succeeded": true,
  "version": 1.0,
  "current_page": 1,
  "per_page": 100,
  "results": [
    {
      "get_info": {
        "fieldA": "4198126",
        ...
      }
   ]
}
于 2016-07-28T21:36:49.183 に答える
1

ツールJSONschema2POJOを使用して、以下の JSON コンテンツのプレーンPOJOを生成することをお勧めします。

POJO の生成中に、ソース タイプJSONとして、注釈スタイルnoneとして選択できます。

{
    "succeeded": {
        "current_page": 1,
        "per_page": 100,
        "results": [
            {
                "get_info": {
                  "fieldA": "4198126",
                  "fieldB": "2016-05-25T22:43:52Z",
                  "fieldC": "iws-user-cfg-proxy-beta",
                  "updated_at": "2016-05-25T22:43:52Z"
                }
            },
            {
                "get_info": {
                  "fieldA": "4551542",
                  "fieldB": "2016-07-27T22:26:27Z",
                  "fieldC": "silkRoot",
                  "updated_at": "2016-07-27T22:26:27Z"
                }
            }
        ]
    }
}

生成された Bean をプロジェクトに追加したら、このオーバーロードされたメソッドをマッパー クラスに追加できます。

private Succeeded succeeded;

/**
 *
 * @return
 *     The succeeded
 */
public Succeeded getSucceeded() {
    return succeeded;
}

/**
 *
 * @param succeeded
 *     The succeeded
 */
public void setSucceeded(Succeeded succeeded) {
    this.succeeded = succeeded;
}
于 2016-07-29T02:28:25.520 に答える