首页 > 解决方案 > Web 服务哈希映射 Java 摆脱

问题描述

我正在尝试将标签名称更改为团队,现在我的 Web 服务正在生成此响应:

    <?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
    <SOAP-ENV:Header/>
    <S:Body>
        <ns2:MapTestResponse xmlns:ns2="http://infomracje/">
            <return>
                <Teams>
                    <item>
                        <Kryptonim>Ratownik 117</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 19</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 154</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 18</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 15</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 17</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 156</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 16</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 111</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 22</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 21</Kryptonim>
                        <Status>lot</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 24</Kryptonim>
                        <Status>lot</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 23</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 20</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>EMS MXH</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>EMS MXI</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 206</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 106</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 406</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 306</Kryptonim>
                        <Status>kon</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 11</Kryptonim>
                        <Status>lot</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 10</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 13</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 12</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 1</Kryptonim>
                        <Status>lot</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 2</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 3</Kryptonim>
                        <Status>tra</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 4</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 5</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 6</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 7</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 8</Kryptonim>
                        <Status>got</Status>
                    </item>
                    <item>
                        <Kryptonim>Ratownik 9</Kryptonim>
                        <Status>got</Status>
                    </item>
                </Teams>
            </return>
        </ns2:MapTestResponse>
    </S:Body>
</S:Envelope>

这是我用来创建地图元素的代码:

public class MapElements {
        @XmlElement public String  Kryptonim;
      @XmlElement public String Status;

      private MapElements() {} //Required by JAXB

      public MapElements(String Kryptonim, String Status)
      {
        this.Kryptonim   = Kryptonim;
        this.Status = Status;
      }

    }

这是一个 MapAdapter 类:

class MapAdapter extends XmlAdapter<MapElements[], Map<String, String>> {
    public MapElements[] marshal(Map<String, String> arg0) throws Exception {
        MapElements[] mapElements = new MapElements[arg0.size()];
        int i = 0;
        for (Map.Entry<String, String> entry : arg0.entrySet())
            mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());

        return mapElements;
    }
public Map<String, String> unmarshal(MapElements[] arg0) throws Exception {
    Map<String, String> r = new HashMap<String, String>();
    for (MapElements mapelement : arg0)
        r.put(mapelement.Kryptonim, mapelement.Status);
    return r;
}



}

最后是根类,我可以将信息从数据库放到

    @XmlRootElement
public class Root
{

  @XmlJavaTypeAdapter(MapAdapter.class)
  private Map<String, String> Teams;

  public Root()
  {
    Teams = new HashMap<String, String>();

  }
    public void TeamAdd(String kryptonim,String status)   { Teams.put(kryptonim, status); }

    }

所以我的问题是如何重命名这个<item>标签?我对 Web 服务 Java 编程相当陌生。

标签: javaweb-serviceshashmap

解决方案


推荐阅读