开发者

Map serialization in JAX-B producing unwanted XML namespaces and prefixes

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-i开发者_Go百科nstance" 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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜