question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InvalidDefinitionException in case of deserialization xml to data class with @JacksonXmlText annotation

See original GitHub issue

Data class example:

@JacksonXmlRootElement(localName = "sms")
@JsonIgnoreProperties(ignoreUnknown = true)
data class Sms(
        @set:JacksonXmlProperty(localName = "Phone", isAttribute = true)
        var phone: String?,

        @JacksonXmlText
        var text: String? = ""
)

Corresponding xml:

<sms Phone="435242423412" Id="43234324">Lorem ipsum</sms>

Deserialization causes such output:

Exception in thread "main" com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Invalid definition for property `` (of type `proton72.github.com.Sms`): Could not find creator property with name '' (known Creator properties: [Phone, text])
 at [Source: (StringReader); line: 1, column: 1]
	at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:62)
	at com.fasterxml.jackson.databind.DeserializationContext.reportBadPropertyDefinition(DeserializationContext.java:1446)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.addBeanProps(BeanDeserializerFactory.java:567)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.buildBeanDeserializer(BeanDeserializerFactory.java:227)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerFactory.createBeanDeserializer(BeanDeserializerFactory.java:137)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:411)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:349)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
	at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
	at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
	at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:477)
	at com.fasterxml.jackson.databind.ObjectMapper._findRootDeserializer(ObjectMapper.java:4178)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3997)

Reproduced on jackson-dataformat-xml and jackson-module-kotlin 2.9.4 both.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:16 (8 by maintainers)

github_iconTop GitHub Comments

12reactions
TWiStErRobcommented, Jan 21, 2020

Outdated, I recommend to use setXMLTextElementName.

Based on the above discovery here’s another workaround (instead of using JsonNode):

data class Attribute(
	@JacksonXmlProperty(isAttribute = true)
	val code: String
) {
	@JacksonXmlText
	lateinit var title: String private set

	override fun toString() = "$code->$title"
}
Feed(attributes=[private->Private Event, public->Public Event])

Note: this is not a solution as the field will be writable (var, private set helps a bit, but still), will have no construct-time validation that the field is set (lateinit), and the field will not play in data class operations like equals, toString, and others (it’s not in the primary constructor)!

8reactions
ctruchicommented, Jan 20, 2020

I just found someone on kotlin slack (named Stephan Schroeder) providing a solution :

You need a mapper like this :

XmlMapper(JacksonXmlModule()
        .apply {
            setXMLTextElementName("text")
        })

and annotate your field like this:

data class Attribute(
	@JacksonXmlProperty(isAttribute = true)
	val code: String,

        @JsonProperty("text")
	@JacksonXmlText
	val title: String?
)

This works for me with jackson 2.10.1. Slack user claims it works with jackson 2.9.9

Read more comments on GitHub >

github_iconTop Results From Across the Web

InvalidDefinitionException in case of deserialization xml to ...
InvalidDefinitionException in case of deserialization xml to data class with @JacksonXmlText annotation.
Read more >
Conflicting getter definitions for property "time" - Stack Overflow
Your InternalTimeDto class needs the JacksonXmlText annotation for the time field: @Getter @Setter @Builder @ToString @NoArgsConstructor ...
Read more >
Jackson Xml Deserialization Invaliddefinitionexception - ADocLib
InvalidDefinitionException in case of deserialization xml to data class with @JacksonXmlText annotation #138.Open.proton72 opened this issue.
Read more >
XML Serialization and Deserialization with Jackson - Javatpoint
By using Jackson, we can easily serialize a Java Object into XML data or ... and deserializing XML data is to create an...
Read more >
Solving the XML Problem with Jackson - Stackify
In order to work with XML, we'll instead use the XmlMapper class. ... Next, let's have a look at the @JacksonXmlText annotation.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found