6

残りのサービスは次のように応答します

<transaction><trxNumber>1243654</trxNumber><type>INVOICE</type></transaction>

またはJSONで:

{"transaction":{"trxNumber":1243654,"type":"INVOICE"}}

私が使用しても問題はありません:

mapper.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true)

そして結果のクラスとして

@JsonRootName("transaction")
public class Transaction {
private String trxNumber;
private String type;
//getters and setters
}

しかし、実際には、サードパーティの jar の Transaction クラスを使用する必要があります。これは上記とまったく同じですが、 @JsonRootName("transaction") 注釈はありません。

だから私はで終わる

Could not read JSON: Root name 'transaction' does not match expected ('Transaction') for type...

Transaction クラス自体に何かを追加せずに Jackson 解析を Transaction クラスに強制する方法はありますか (このファイルをバイナリ jar の一部として取得するため)。

カスタムの PropertyNamingStrategy を試してみましたが、フィールド名とゲッター/セッター名のみに関係しているようで、クラス名には関係ないようです。

Java7、ジャクソン 2.0.5。

助言がありますか?ありがとう。

4

1 に答える 1

5

ミックスイン機能でそれを行うことができます。次のような単純なインターフェイス/抽象クラスを作成できます。

@JsonRootName("transaction")
interface TransactionMixIn {

}

ObjectMapper次に、オブジェクトを構成する必要があります。

ObjectMapper mapper = new ObjectMapper();
mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
mapper.addMixInAnnotations(Transaction.class, TransactionMixIn.class);

最後に、それを使用して JSON を逆シリアル化できます。

mapper.readValue(json, Transaction.class);

2 番目のオプション - クラスのカスタム デシリアライザーを作成できますTransaction

于 2013-10-25T22:42:29.960 に答える