Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I'm working with the Jackson XML plugin (
https://github.com/FasterXML/jackson-dataformat-xml
), and I'm not sure if it's supported, but I'm wondering if it's possible to both serialize and deserialize XML with namespace prefixes, like so:
<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 Jackson's plugin like so:
@JacksonXmlProperty(localName="name:Bar")
public Bar getBar() {
return bar;
However, I can't find a way to configure my POJOs to deserialize from the XML generated. Please see the following example code:
public class Bar{
@JacksonXmlProperty(localName="name:x")
public String x = "x";
@JacksonXmlProperty(localName="name:y")
public String y = "y";
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's a way to get this to work with Jackson XML?
JacksonXmlProperty
annotation has property namespace
. Use it for defining 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;
–
–
–
Though this is an old post, I just ran into this issue and the only solution to match the XML required for my service calls was something like this, where the namespace declaration also uses the prefix.
@JacksonXmlRootElement(localName = "entry")
abstract class BaseEntry implements Serializable {
private static final long serialVersionUID = 6835429382441010779L;
private final String xmlns = "com.foo.bar";
private String type;
protected BaseEntry(String type) {
this.type = type;
@JacksonXmlProperty(isAttribute = true)
public String getType() {
return type;
@JacksonXmlProperty(localName = "xmlns:ns2", isAttribute = true)
public String getXmlns() {
return xmlns;
This produces something like:
<entry type="com.foo.bar" xmlns:ns2="com.foo.bar">
</entry>
Configuring the xmlMapper
to ignore the namespace could avoid this exception.
XMLInputFactory input = new WstxInputFactory();
input.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE);
XmlMapper xmlMapper = new XmlMapper(new XmlFactory(input, new WstxOutputFactory()));
Comes from this answer.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.