简体   繁体   English

如何使用泽西岛封送地图

[英]How to marshal unmarshal a Map using Jersey

I have seen a lot of examples where a Map is passed as an object in a class and annotated with a custom XMLJavaAdapter which is used for marshalling/unmarshalling the Map. 我已经看到了很多示例,其中Map作为类中的对象传递,并使用自定义XMLJavaAdapter进行批注,该XMLJavaAdapter用于对Map进行编组/解组。 But I am trying to pass a Map itself as the requestedEntity in a POST request and the response also as a Map and not a class containing a Map for which I can see numerous solutions.. 但是我试图在POST请求中将Map本身作为requestEntity传递,并将响应也作为Map传递,而不是将包含Map的类传递给我,因此我可以看到很多解决方案。

Input Class(Requested Entity): GenericMap.java 输入类别(请求的实体): GenericMap.java

import java.util.HashMap;
import javax.xml.bind.annotation.XmlRootElement;

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

@XmlRootElement
@XmlJavaTypeAdapter(GenericMapAdapter.class)
public class GenericMap<K,V> extends HashMap<K,V> {

}

GenericMapAdapter.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 MapType.java

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;
    }
}

MapElementsType.java MapElementsType.java

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;
  }

}

When I make genericmap as an member variable of a class and annotate it with GenericMapAdapter, it works fine. 当我将genericmap用作类的成员变量并用GenericMapAdapter对其进行注释时,它可以正常工作。 But, I want GenericMap itself to be passed as input requested entity. 但是,我希望将GenericMap本身作为输入请求的实体传递。 And when i try that, i see an empty xml request in my logs and 400 Bad Request : 当我尝试这样做时,我在日志中看到一个空的xml请求,并看到400 Bad Request:

I think the solution to your problem goes through not using JAXB and doing manual mapping of the request params or response: 我认为解决问题的方法是不使用JAXB,而是手动映射请求参数或响应:

See In Jersey, how do I make some methods use the POJO mapping feature and some not? 请参见In Jersey,如何使某些方法使用POJO映射功能而有些则不?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM