InvalidDefinitionException in case of deserialization xml to data class with @JacksonXmlText annotation
See original GitHub issueData 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:
- Created 6 years ago
- Reactions:5
- Comments:16 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Outdated, I recommend to use
setXMLTextElementName
.Based on the above discovery here’s another workaround (instead of using JsonNode):
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 indata class
operations likeequals
,toString
, and others (it’s not in the primary constructor)!I just found someone on kotlin slack (named Stephan Schroeder) providing a solution :
You need a mapper like this :
and annotate your field like this:
This works for me with jackson 2.10.1. Slack user claims it works with jackson 2.9.9