java - Jackson FasterXML POJO to XML list -
i'm using fasterxml jackson implementation convert pojo's xml output xml-databing package. i'm trying achieve output:
<myrequest> <mysubrequest>4</mysubrequest> <mysubrequest>5</mysubrequest> </myrequest>
my classes:
public class mysubrequest { @jacksonxmltext private string id; public string getid() { return id; } public void setid(string id) { this.id = id; } public mysubrequest(string id) { super(); this.id = id; } }
and:
@jacksonxmlrootelement public class myrequest { private collection<mysubrequest> mysubrequest; public collection<mysubrequest> getrequests() { return mysubrequest; } public void setrequests(collection<mysubrequest> requests) { this.mysubrequest = requests; } }
i'm testing with:
objectmapper mapper = new xmlmapper(); myrequest entity = new myrequest(); collection<mysubrequest> myids = new arraylist<>(); myids.add(new mysubrequest("12")); myids.add(new mysubrequest("34")); entity.setrequests(myids); mapper.writevalue(system.out, entity);
but output is:
<myrequest xmlns=""> <requests> <requests>12</requests> <requests>34</requests> </requests> </myrequest>
another thing i'd know how force output case-sensitive i.e. uppercase variable names.
you can use jacksonxmlelementwrapper
annotation ignore wrapper. use :
@jacksonxmlrootelement class myrequest { private collection<mysubrequest> mysubrequest; public collection<mysubrequest> getrequests() { return mysubrequest; } @jacksonxmlproperty(localname = "mysubrequest") @jacksonxmlelementwrapper(usewrapping = false) public void setrequests(collection<mysubrequest> requests) { this.mysubrequest = requests; } }
here have used jacksonxmlproperty
annotation use element name "mysubrequest" in xml.
Comments
Post a Comment