2

Problem:

I'm trying to do a simple serialization of a HashMap with JAX-B in a JAX-RS application and running into extra output that I'd like to avoid. The default serialization of the HashMap includes XML namespaces and prefixes that are useless (for my app).

The output I'm getting for the Map is:

<params>
  <entry>
    <key xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">keyName</key>
    <value xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">123</value>
  </entry>
  ...
</params>

instead of:

<params>
  <entry>
    <key>keyName</key>
    <value>123</value>
  </entry>
  ...
</params>

The class is basically laid out like so:

@XmlRootElement(name="example")
public ExampleClass
{
  private params HashMap<String,Object> = new HashMap<String,Object>();

  public ExampleClass() { }

  @XmlElementWrapper(name="params", required=true)
  public Map getParameters()
  {
    return params;
  }
}

What can be done to simplify the XML output?

Library reference:

  • JAX-RS (Resteasy 2.0, not married to this version)
  • JAX-B (included in Resteasy 2.0)
A. R. Younce
  • 1,913
  • 17
  • 22

1 Answers1

4

Since your map doesn't use generics the serializer writes the data type of each element value.

Try using:

public Map<String,String> getParameters()

Even if you use Map<String,Object> the serializer has to write the corresponding type of the value element.