0

How do I annotate a class that extends a HashMap for XML Marshalling using JAXB.

This example code

@XmlAccessorType(XmlAccessType.FIELD)
public class ExampleMap extends HashMap<String,String>{

   public String group= "Test Group";

}

Produces this xml

<ExampleMap>
  <group>Test Group</group>
</ExampleMap>

I can modify the class to this

@XmlAccessorType(XmlAccessType.FIELD)
public class ExampleMap extends HashMap<String,String>{

    public String group= "Test Group";
    public Map<String,String> self = this;

}

and I now get

<ExampleMap>
  <group>Test Group</group>
  <self>
    <entry><key>2</key><value>2</value></entry>
    <entry><key>1</key><value>1</value></entry>
  </self>
</ExampleMap>

Which is the result I want, but using that self referencing Map<String,String> self = this; feels wrong.

How should it be done?

muttonUp
  • 6,351
  • 2
  • 42
  • 54

1 Answers1

0

Turns out that although it felt wrong, it is in fact a bonafide way of doing it.

Although this answer https://stackoverflow.com/a/4152683/3696510 uses a get method to return 'this' the results are the same.

Community
  • 1
  • 1
muttonUp
  • 6,351
  • 2
  • 42
  • 54