Salut,
J'ai un souci avec JAXB via un webservice.
J'ai un pojo qui prend une map <String, String>
J'ai adapte un bout de code sur le net.
Cela me donne :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 @XmlRootElement public class UserProfile implements Serializable{ private static final long serialVersionUID = 1L; @XmlJavaTypeAdapter(MapAdapter.class) private Map<String, String> properties ; public UserProfile(){ this.properties = new HashMap<String, String>(); } public void setProperty(String name, String value){ this.properties.remove(name); this.properties.put(name, value); } public String getProperty(String name, String defaultValue){ String v = this.properties.get(name); if( v == null ){ return defaultValue ; } else { return v ; } } public String getProperty(String name){ String v = this.properties.get(name); if( v == null ){ return "" ; } else { return v ; } } public List<String> getProperties(String name){ List<String> strings = new ArrayList<String>(); String v = this.properties.get(name); if( v != null){ v.replaceAll(" *, *", ","); strings.addAll(Arrays.asList(v.split(","))); } return strings ; } public void setProperties(String name, List<String> values){ String buf = ""/*new StringBuilder()*/; for (String c : values) { if (buf.length() > 0) { buf += ","; } buf.concat(c.toString()); } this.setProperty(name, buf.toString()); } public boolean isSet( String name ){ return this.properties.get(name) != null ; } public Collection<String> getPropertyNames(){ return this.properties.keySet() ; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 public class MapAdapter extends XmlAdapter<MapElements[], Map<String, String>> { public MapAdapter (){}//Required by JAXB private MapElements[] tabOfMapElements; public MapElements[] marshal(Map<String, String> map) throws Exception{ MapElements[] mapElements = new MapElements[map.size()]; int i = 0; for (Map.Entry<String, String> entry : map.entrySet()) mapElements[i++] = new MapElements(entry.getKey(), entry.getValue()); return mapElements; } public Map<String, String> unmarshal(MapElements[] tab) throws Exception{ Map<String,String> r = new HashMap<String,String>(); for(MapElements mapelement : tab) r.put(mapelement.key, mapelement.value); return r; } }Le probleme vient du fait que j'ai besoin de rattacher cette map a mon userProfile, or je ne me retrouve plus avec une liste de MapElement,
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 public class MapElements { @XmlElement public String key; @XmlElement public String value; private MapElements() {} //Required by JAXB public MapElements(String key, String value) { this.key = key; this.value = value; } }
setProperties attent une MapElementsArray, un truc genere.
Du coup je me retrouve le bec dans l'eau
Si quelqu'un sait d'ou vient le probleme
Merci
Partager