Jackson XML - XML ​​deserialization with namespace prefixes

I am working with the Jackson XML plugin ( https://github.com/FasterXML/jackson-dataformat-xml ) and I'm not sure if it was supported by m wondering whether it is possible to serialize and deserialize XML with namespace prefixes , eg:

<name:Foo> <name:Bar> <name:x>x</name:x> <name:y>y</name:y> </name:Bar> </name:Foo> 

I can generate this type of XML using the Jackson plugin as follows:

 @JacksonXmlProperty(localName="name:Bar") public Bar getBar() { return bar; } 

However, I cannot find a way to configure my POJOs to deserialize from the generated XML. See the following code example:

 public class Bar{ @JacksonXmlProperty(localName="name:x") public String x = "x"; @JacksonXmlProperty(localName="name:y") public String y = "y"; } 

 @JacksonXmlRootElement(localName="name:Foo") public class Foo{ private Bar bar; @JacksonXmlProperty(localName="name:Bar") public Bar getBar() { return bar; } public void setBar(Bar bar) { this.bar = bar; } } 

 public class TestDeserialization { public static void main(String[] args) throws Exception { Foo foo = new Foo(); foo.setBar(new Bar()); XmlMapper xmlMapper = new XmlMapper(); String xml = xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsString(foo); System.out.println(xml); System.out.println("XML Desearialzing...."); Foo foo2= xmlMapper.readValue(xml, Foo.class); System.out.println(xmlMapper.writeValueAsString(foo2)); } } 

Trying to run this test gives me an exception:

 Exception in thread "main" java.io.IOException: com.ctc.wstx.exc.WstxParsingException: Undeclared namespace prefix "name" 

which is understandable, but I was wondering if there is a way to get this to work with Jackson XML?

+4
source share
1 answer

JacksonXmlProperty annotation has a namespace property. Use it to determine namespace

 public class Bar { @JacksonXmlProperty(namespace = "name",localName="x") public String x = "x"; @JacksonXmlProperty(namespace = "name",localName="y") public String y = "y"; } @JacksonXmlRootElement(namespace = "name", localName = "Foo") public class Foo { private Bar bar; @JacksonXmlProperty(namespace = "name", localName = "Bar") public Bar getBar() { return bar; } public void setBar(Bar bar) { this.bar = bar; } } 
+2
source

Source: https://habr.com/ru/post/1479752/


All Articles