私は、Jackson FasterXMLを使用してオブジェクトに変換したいXMLをいくつか持っています。複数のラッパークラスを使用せずにXMLをどのように非直列化しますか?
JsonIgnoreProperties(ignoreUnknown = true)
@JacksonXmlRootElement(localName = "services")
public class ServiceWrapper {
@JacksonXmlProperty(localName = "service")
private Service service;
//Getters and setters [...]
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Service {
@JacksonXmlProperty(isAttribute = true)
private int id;
@JacksonXmlProperty(isAttribute = true)
private String name;
@JacksonXmlProperty(localName = "addresses")
private AddressWrapper addresses;
//Getters and setters [...]
}
public class AddressWrapper {
@JacksonXmlProperty(localName = "postalAddress")
private List<Address> addresses;
//Getters and setters [...]
}
@JsonIgnoreProperties(ignoreUnknown = true)
public class Address {
@JacksonXmlProperty(isAttribute = true, localName = "id")
private int id;
@JacksonXmlProperty(isAttribute = true, localName = "line1")
private int address1;
@JacksonXmlProperty(isAttribute = true, localName = "city")
private int city;
//Getters and setters [...]
}
とマッピングを行うコード:
JacksonXmlModule module = new JacksonXmlModule();
module.setDefaultUseWrapper(false);
ObjectMapper mapper = new XmlMapper(module);
mapper.registerModule(new JSR310Module());
mapper.configure(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS, true);
ServiceWrapper serviceWrapper = mapper.readValue(xmlString, ServiceWrapper.class);
return serviceWrapper.getService();
このすべての作品、私はこれらのクラスで成功したオブジェクトとしてこれをデシリアライズしています
<services>
<service id="1" name="test">
<addresses>
<postalAddress id="2" line1="123 Fake Street" city="Springfield" />
</addresses>
</service>
</services>
:XMLは次のようになりますしかし、ServiceWrapperクラスとAddressWrapperクラスを持つのはオーバーヘッドで醜いコードです。実際に私が望むのは、<service>
ノードと<postalAddress>
ノードのデータだけです。 <addresses>
ノードを表すAddressWrapperクラスを持たずに、Jacksonクラスのオブジェクトに直接、サービスクラスのアドレスリストを設定することはできますか?同様に、XML全体を取得し、ServiceWrapperクラスを必要とせずに直接Serviceクラスを生成するには?