添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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;
                Using the namespace configuration like above generates the following XML: <Foo xmlns="name">   <Bar>     <x>x</x>     <y>y</y>   </Bar> </Foo> - Can I assume this is synonymous with the sample XML I gave before?
– jtyler
                May 8, 2013 at 15:00
                @Ilya: I am facing the same issue as in this question. I am also getting <Foo xmlns="name"> <Bar> <x>x</x> <y>y</y> </Bar> </Foo>. How is this same as <name:foo></name:foo> ?? The prefix is not getting added. Can you elaborate ?
– Siddharth Trikha
                Sep 6, 2016 at 4:17
                @SiddharthTrikha my understanding is either you use xmlns directly with a name as you stated <Foo xmlns="name"> which means the Foo tag is defined in the "name" namespace, or you give an alias to this namespace and use it like <mynamespace:Foo xmlns:mynamespace="name">
– Christophe
                Jun 11, 2020 at 10:33

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.