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. 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..
Input Class(Requested Entity): 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
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
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
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. But, I want GenericMap itself to be passed as input requested entity. And when i try that, i see an empty xml request in my logs and 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:
See In Jersey, how do I make some methods use the POJO mapping feature and some not?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.