JAXB, marshalling a subclass that has the same rootNode as the superclass

Say I have this:

public class Foo {
  private String value;

  // <snip> getters and setters, constructors etc
}

And I also have this:

public class Bar extends Foo {
  private String anotherValue;

  // <snip> getters and setters, constructors etc
}

I want this to be able to bind to an object Bar:

<foo>
  <value>smang</value>
  <anotherValue>wratz</anotherValue>
</foo>

I can’t check right now, but if I change the name @XmlRootNode Barto 'foo' and just pass it Bar.classto the JAXB marshaller, will this work? Do I need to do something smarter?

+3
source share
1 answer

I tried to do the same, found the answer here:

JAXB sorts declared parent class against actual runtime subclass

This works for me, hope it helps!

+1
source

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


All Articles