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