Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用Jersey编组解组地图_Java_Json_Jersey_Marshalling_Unmarshalling - Fatal编程技术网

Java 如何使用Jersey编组解组地图

Java 如何使用Jersey编组解组地图,java,json,jersey,marshalling,unmarshalling,Java,Json,Jersey,Marshalling,Unmarshalling,我见过很多例子,其中一个映射作为类中的对象传递,并使用一个自定义XMLJavaAdapter进行注释,该适配器用于编组/解编组映射。但是我试图在POST请求中将映射本身作为requestedEntity传递,并且将响应也作为映射传递,而不是一个包含映射的类,我可以看到很多解决方案 输入类请求的实体: GenericMap.java GenericMapAdapter.java import java.util.HashMap; import java.util.Map; import java.

我见过很多例子,其中一个映射作为类中的对象传递,并使用一个自定义XMLJavaAdapter进行注释,该适配器用于编组/解编组映射。但是我试图在POST请求中将映射本身作为requestedEntity传递,并且将响应也作为映射传递,而不是一个包含映射的类,我可以看到很多解决方案

输入类请求的实体: GenericMap.java

GenericMapAdapter.java

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.annotation.adapters.XmlAdapter;


 public class GenericMapAdapter<K, V> extends XmlAdapter<MapType<K,V>, Map<K,V>> {

    @Override
    public MapType marshal(Map<K,V>  map) throws Exception {
        MapType<K,V> mapElements = new MapType<K,V>();        

        for (Map.Entry<K, V> entry : map.entrySet()){
            MapElementsType<K,V> mapEle = new MapElementsType<K,V>      (entry.getKey(),entry.getValue());
            mapElements.getEntry().add(mapEle);
        }
        return mapElements;
    }

    @Override
    public Map<K, V> unmarshal(MapType<K,V> arg0) throws Exception {
        Map<K, V> r = new HashMap<K, V>();
        K key;
        V value;
        for (MapElementsType<K,V> mapelement : arg0.getEntry()){
            key =mapelement.key;
            value = mapelement.value;
           r.put(key, value);
        }
        return r;

    }
}
MapType.java

maplementstype.java

当我将genericmap作为类的成员变量并使用GenericMapAdapter对其进行注释时,它可以正常工作。但是,我希望GenericMap本身作为输入请求实体传递。当我尝试这样做时,我在日志中看到一个空的xml请求和400个错误的请求:


我认为解决您问题的方法是不使用JAXB并手动映射请求参数或响应:

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import javax.xml.bind.annotation.adapters.XmlAdapter;


 public class GenericMapAdapter<K, V> extends XmlAdapter<MapType<K,V>, Map<K,V>> {

    @Override
    public MapType marshal(Map<K,V>  map) throws Exception {
        MapType<K,V> mapElements = new MapType<K,V>();        

        for (Map.Entry<K, V> entry : map.entrySet()){
            MapElementsType<K,V> mapEle = new MapElementsType<K,V>      (entry.getKey(),entry.getValue());
            mapElements.getEntry().add(mapEle);
        }
        return mapElements;
    }

    @Override
    public Map<K, V> unmarshal(MapType<K,V> arg0) throws Exception {
        Map<K, V> r = new HashMap<K, V>();
        K key;
        V value;
        for (MapElementsType<K,V> mapelement : arg0.getEntry()){
            key =mapelement.key;
            value = mapelement.value;
           r.put(key, value);
        }
        return r;

    }
}
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MapType<K, V> {

    private List<MapElementsType<K, V>> entry = new ArrayList<MapElementsType<K, V>>();

    public MapType() {
    }

    public MapType(Map<K, V> map) {
        for (Map.Entry<K, V> e : map.entrySet()) {
            entry.add(new MapElementsType<K, V>(e.getKey(),e.getValue()));
        }
    }

    public List<MapElementsType<K, V>> getEntry() {
        return entry;
    }

    public void setEntry(List<MapElementsType<K, V>> entry) {
        this.entry = entry;
    }
}
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class MapElementsType<K,V>
{
  @XmlElement public K  key;
  @XmlElement public V value;

  public MapElementsType() {} //Required by JAXB

  public MapElementsType(K key, V value)
  {
    this.key   = key;
    this.value = value;
  }

}