kiedy dodam adnotacje @XmlElementWrapper do moje POJO dostaję wyjątek Exception :
Exception in thread "main" javax.xml.bind.JAXBException:
Exception Description: XmlElementWrapper is only allowed on a collection or array property but [movies] is not a collection or array property.
- with linked exception:
[Exception [EclipseLink-50015] (Eclipse Persistence Services - @VERSION@.@QUALIFIER@): org.eclipse.persistence.exceptions.JAXBException
Exception Description: XmlElementWrapper is only allowed on a collection or array property but [movies] is not a collection or array property.]
at org.eclipse.persistence.jaxb.JAXBContext$TypeMappingInfoInput.createContextState(JAXBContext.java:1068)
at org.eclipse.persistence.jaxb.JAXBContext.<init>(JAXBContext.java:182)
Na to chwilę nie jestem pewien czy potrzebuję tej adnotacji.
Za jaką kolwiek pomoc będę strasznie wdzięczny!!!
Na chwilę obecną dostaję taki kod wynikowy :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<issue>
<custom_fields>
<item id="1">
<value>213</value>
</item>
<item id="2">
<value>4356</value>
</item>
</custom_fields>
<description>adam</description>
<journals/>
<subject>Subject</subject>
</issue>
a chciałbym otrzymać :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<issue>
<custom_fields>
<custom_field id="1">
<value>213</value>
</custom_field>
<custom_field id="2">
<value>4356</value>
</custom_field>
</custom_fields>
<description>adam</description>
<journals/>
<subject>Subject</subject>
</issue>
Moje klasy:
Issue.xml
@XmlRootElement(name = "issue")
public class Issue {
@Expose
private Integer id;
private TypAplikacji project_id;
private TypIncydentu tracker_id;
@Expose
private StatusZgloszenia status;
private Author author;
private String subject;
private String description;
private Map<CustomFields, String> customFields;
@XmlElement(name = "custom_fields")
@XmlJavaTypeAdapter(MapAdapter.class)
public Map<CustomFields, String> getCustomFields() {
return customFields;
}
public void setCustomFields(Map<CustomFields, String> customFields) {
this.customFields = customFields;
}
}
MapElements.java :
@XmlRootElement(name = "custom_field")
public class MapElements {
private CustomFields key;
private String value;
public MapElements() {
}
public MapElements(CustomFields key, String value) {
this.key = key;
this.value = value;
}
@XmlAttribute(name = "id")
public CustomFields getKey() {
return key;
}
public void setKey(CustomFields key) {
this.key = key;
}
@XmlElement(name="value")
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
MapAdapter.java
public class MapAdapter extends XmlAdapter<MapElements[], Map<CustomFields, String>> {
@Override
public Map<CustomFields, String> unmarshal(MapElements[] elements) throws Exception {
HashMap<CustomFields, String> mapElements = new HashMap<>();
for (MapElements element : elements) {
if(!element.getValue().isEmpty())
mapElements.put(element.getKey(), element.getValue());
}
return mapElements;
}
@Override
public MapElements[] marshal(Map<CustomFields, String> elements) throws Exception {
MapElements[] mapElements = new MapElements[elements.size()];
int i = 0;
for (Map.Entry<CustomFields, String> entry : elements.entrySet()) {
mapElements[i++] = new MapElements(entry.getKey(), entry.getValue());
}
return mapElements;
}
}
Test.java :
public class convertObjectToXml {
public static void main(String[] args) throws JAXBException {
Map<CustomFields, String> hashMap = new HashMap<>();
hashMap.put(CustomFields.CODE, "213");
hashMap.put(CustomFields.PHONE, "4356");
Issue issue = new Issue();
issue.setDescription("adam");
issue.setSubject("Subject");
issue.setCustomFields(hashMap);
System.out.println(convertObjectToXml(issue));
}
}
CustomFields is Enum :
public enum CustomFields {
@XmlEnumValue("1")
CODE(1),
@XmlEnumValue("2")
PHONE(2),
....
}