2

XMLまたはJSONを使用してGETメソッドでHashMap>型のオブジェクトを返したい。クライアント側では、マップのキーを取得しますが、値は null です。

これはリターンクラスです:

private HashMap<String, ArrayList<String>> hashMap = new HashMap<>();

public HashMap<String, ArrayList<String>> getHashMap() {
    return hashMap;
}

public void setHashMap(HashMap<String, ArrayList<String>> hashMap) {
    this.hashMap = hashMap;
}

これはサービス内の GET メソッドです。

@GET
@Path("/mapa")
@Produces(MediaType.APPLICATION_XML)
public Test mapa() {

    Test test = new Test();

    HashMap<String, ArrayList<String>> hashMap = new HashMap<>();

    ArrayList<String> list = new ArrayList<>();
    list.add("first");
    list.add("second");

    hashMap.put("first", list);
    test.setHashMap(hashMap);

    return test;
}

私はブラウザでこれを取得します:

<test>
  <hashMap>
    <entry>
      <key>first</key>
      <value/>
    </entry>
  </hashMap>
</test>

値が空なのはなぜですか?

4

1 に答える 1

0

Test Class の代わりに Response を使用しようとしましたか?

@GET
@Path("/mapa")
@Produces(MediaType.APPLICATION_XML)
public Response mapa() {

    Test test = new Test();

    HashMap<String, ArrayList<String>> hashMap = new HashMap<>();

    ArrayList<String> list = new ArrayList<>();
    list.add("first");
    list.add("second");

    hashMap.put("first", list);
    test.setHashMap(hashMap);

    return Response.status(200).entity(test).build();
} 
于 2012-07-23T09:26:06.237 に答える