我能够编写XML文档,但是无法对创建的XML进行反序列化。原始代码在Kotlin中,但是我发布了Java等价物。
@JacksonXmlRootElement(localName = "assets")
public class Assets {
@JacksonXmlElementWrapper(useWrapping = false)
private Asset[] asset;
public Assets(Asset[] asset) {
this.asset = asset;
}
public Asset[] getAsset() {
return asset;
}
public void setAsset(Asset[] asset) {
this.asset = asset;
}
}
public class Asset {
@JacksonXmlProperty(isAttribute = true)
private String type;
@JacksonXmlProperty(isAttribute = true)
private String name;
@JacksonXmlProperty(isAttribute = true)
private String displayName;
@JacksonXmlElementWrapper(localName = "permissions")
private Permission[] permission;
public Asset(String type, String name, String displayName, Permission[] permission) {
this.type = type;
this.name = name;
this.displayName = displayName;
this.permission = permission;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public Permission[] getPermission() {
return permission;
}
public void setPermission(Permission[] permission) {
this.permission = permission;
}
}
public class Permission {
@JacksonXmlProperty(isAttribute = true)
private String name;
private Group[] group;
public Permission(String name, Group[] group) {
this.name = name;
this.group = group;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Group[] getGroup() {
return group;
}
public void setGroup(Group[] group) {
this.group = group;
}
}
用(Kotlin)实例化:
fun writeToXml(obj: Any) : String {
val xmlMapper = XmlMapper()
xmlMapper.enable(SerializationFeature.INDENT_OUTPUT)
return xmlMapper.writeValueAsString(obj)
}
val assets = Assets(arrayOf(
Asset("bdo", "approval-matrix", "Approval Matrix", arrayOf(Permission("Create", arrayOf(Group(arrayOf("Admin", "Approver")))))),
Asset("bdo", "approval-matrix-2", "Approval Matrix 2", arrayOf(Permission("Delete", arrayOf(Group(arrayOf("Admin", "Approver")), Group(arrayOf("Admin-2", "Approver-2"))))))))
val xmlModule = JacksonXmlModule()
val objectMapper = XmlMapper(xmlModule)
println(writeToXml(assets))
生成的XML符合预期:
<assets>
<asset type="bdo" name="approval-matrix" displayName="Approval Matrix">
<permissions>
<permission name="Create">
<groups>
<group>Admin</group>
<group>Approver</group>
</groups>
</permission>
</permissions>
</asset>
<asset type="bdo" name="approval-matrix-2" displayName="Approval Matrix 2">
<permissions>
<permission name="Delete">
<groups>
<group>Admin</group>
<group>Approver</group>
</groups>
<groups>
<group>Admin-2</group>
<group>Approver-2</group>
</groups>
</permission>
</permissions>
</asset>
</assets>
但是在尝试反序列化时:
val generated = objectMapper.readValue<Assets>(xmlAssets)
我收到以下错误:
Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `uk.co.processflows.configuration.entities.Assets` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (StringReader); line: 2, column: 3]
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1290)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
at com.fasterxml.jackson.dataformat.xml.deser.WrapperHandlingDeserializer.deserialize(WrapperHandlingDeserializer.java:113)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3011)
at uk.co.processflows.module.EntryPoint.main(EntryPoint.kt:58)
任何帮助都将不胜感激。
答案 0 :(得分:0)
现在有一个Jackson模块允许它反序列化到Kotlin数据类,而无需添加默认构造函数。
请参阅https://github.com/FasterXML/jackson-module-kotlin
compile "com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+"
尝试将此添加到您的Kotlin项目中,以便您可以保持域类“纯粹”。
答案 1 :(得分:0)
只是为了阐明答案:
我包括了
dependencies {
...
compile(kotlin("reflect"))
compile("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.+")
compile("com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.+")
...
}
在build.gradle.kts
然后有同样的问题。通过向构造函数添加默认值来更改数据类似乎可以解决该问题。 IE:
data class HelloWorld(val hello: Boolean, val world: Boolean)
<-错误
成为
data class HelloWorld(val hello: Boolean = false, val world: Boolean = false)
<-工作中
然后:
const val xml = """
<HelloWorld>
<hello>true</hello>
<world>true</world>
</HelloWorld>
"""
通常会通过数据类在kotlin中进行序列化和反序列化。谢谢!