java - JAXB List as top level XML -
update: tried suggested here , seemed have progress api on engineered intended purpose , ran out of time, switched hard coding "student-list" in string response of /student call more enough proof of concept needed for.
i working on simple rest service uses xml work student records.
when on /student get:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <students> <student> <grade>b</grade> <name>jane</name> </student> ... </students>
however, output be:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <student-list> <student> <grade>b</grade> <name>jane</name> </student> ... </student-list>
here student.java snippet:
@xmlrootelement(name = "student") public class student { private string name; private string grade; public student() { } public student(string name, string grade) { super(); this.name = name; this.grade = grade; } // getters/setters ... }
i have tried change xmlrootelement "student-list", makes every sub element of list student-list type, , ignores top level element list.
the project uses jax-rs studentsresource class have tried setting xmlrootelement name on resource class, did not affect output.
it seems automatically naming top level list based on studentsresource class, , cannot figure out how override name.
i have been combing documentation on java not strongest language these days having trouble understanding how pieces fit jaxb , jax-rs.
any ideas, or push in right direction ?
edit: added studentsresource.java snippet:
@path("/student") public class studentsresource { // mapper class @context uriinfo uriinfo; @context request request; studentservice studentservice; public studentsresource() { studentservice = new studentservice(); } @get @produces("text/xml;charset=utf-8") public list<student> getstudents() { return studentservice.getstudentaslist(); } ... }
based on mateen momin's feedback, have outputting rest service on /student:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <studentdao_students> <student> <name>jane</name> <grade>b</grade> </student> ... </studentdao_students>
i have studentdao, gets wrapped studentservice, gets called resource classes make http calls possible. have moved student class studentdao, made me realize studentservice getting marshalled, here updated snippets:
studentdao.java:
public enum studentdao { instance; private map<string, student> students = new hashmap<string, student>(); private studentdao() { //pre-populate testing purposes student student = new student("john", "a"); students.put("john", student); student = new student("jane", "b"); students.put("jane", student); student = new student("testuser", "f"); students.put("testuser", student); } public map<string, student> getstudents() { return students; } @xmlrootelement(name="student") @xmlaccessortype(xmlaccesstype.field) public static class student { private string name; private string grade; public student() { } public student(string name, string grade) { super(); this.name = name; this.grade = grade; } // getters/setters here... } }
studentservice.java:
@xmlrootelement(name = "student-list") @xmlaccessortype(xmlaccesstype.field) public class studentservice { studentdao studentdao; public studentservice() { studentdao = studentdao.instance; } // crud methods here call studentdao hashmap }
studentsresource.java:
@path("/student") public class studentsresource { @context uriinfo uriinfo; @context request request; studentservice studentservice; public studentsresource() { studentservice = new studentservice(); } @get @produces("text/xml;charset=utf-8") public list<student> getstudents() { return studentservice.getstudentaslist(); } // other http calls, post,put,etc. here }
i created main function see happens if specify jaxbcontext:
main testing console output:
public class main { public static void main(string[] args) { studentservice studentdao = new studentservice(); studentdao.student stu1 = new studentdao.student(); stu1.setname("marc"); stu1.setgrade("a"); studentdao.student stu2 = new studentdao.student(); stu2.setname("jacob"); stu2.setgrade("b"); studentdao.student stu3 = new studentdao.student(); stu3.setname("anthony"); stu3.setgrade("a"); studentdao.createstudent(stu1); studentdao.createstudent(stu2); studentdao.createstudent(stu3); try { file file = new file("out.xml"); jaxbcontext jaxbcontext = jaxbcontext.newinstance(studentservice.class); marshaller jaxbmarshaller = jaxbcontext.createmarshaller(); // output pretty printed jaxbmarshaller.setproperty(marshaller.jaxb_formatted_output, true); jaxbmarshaller.marshal(studentdao, file); jaxbmarshaller.marshal(studentdao, system.out); } catch (jaxbexception e) { e.printstacktrace(); } } }
when run main function see output this, not match rest output of course:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <student-list> <studentdao>instance</studentdao> </student-list>
which seems promising, cannot seem top level class name overridden in rest calls, need.
any other ideas of i'm doing wrong here ?
students class
import java.util.arraylist; import javax.xml.bind.annotation.xmlaccesstype; import javax.xml.bind.annotation.xmlaccessortype; import javax.xml.bind.annotation.xmlelement; import javax.xml.bind.annotation.xmlrootelement; @xmlrootelement(name = "student-list") @xmlaccessortype(xmlaccesstype.field) public class students{ //students @xmlelement(name = "student") public arraylist<student> studentslst; //getters , setters public arraylist<student> getstudentslst() { return studentslst; } public void setstudentslst(arraylist<student> studentslst) { this.studentslst = studentslst; } @xmlrootelement @xmlaccessortype(xmlaccesstype.field) public static class student { private string name; private string grade; public student() { } public student(string name, string grade) { super(); this.name = name; this.grade = grade; } // getters/setters ... public string getname() { return name; } public void setname(string name) { this.name = name; } public string getgrade() { return grade; } public void setgrade(string grade) { this.grade = grade; } } }
main method /marshaller use:
public static void main(string[] args) { students.student stu1 = new students.student(); stu1.setname("marc"); stu1.setgrade("a"); students.student stu2 = new students.student(); stu2.setname("jacob"); stu2.setgrade("b"); students.student stu3 = new students.student(); stu3.setname("anthony"); stu3.setgrade("a"); arraylist<students.student> studentslst = new arraylist<>(); studentslst.add(stu1); studentslst.add(stu2); studentslst.add(stu3); students students = new students(); students.setstudentslst(studentslst); try { file file = new file("c:\\file.xml"); jaxbcontext jaxbcontext = jaxbcontext.newinstance(students.class); marshaller jaxbmarshaller = jaxbcontext.createmarshaller(); // output pretty printed jaxbmarshaller.setproperty(marshaller.jaxb_formatted_output, true); jaxbmarshaller.marshal(students, file); jaxbmarshaller.marshal(students, system.out); } catch (jaxbexception e) { e.printstacktrace(); } }
output generated:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <student-list> <student> <name>marc</name> <grade>a</grade> </student> <student> <name>jacob</name> <grade>b</grade> </student> <student> <name>anthony</name> <grade>a</grade> </student> </student-list>
ps: since using rest, main method given above can used fyi. use main method build studentservice.getstudentaslist()
method accordingly. (my assumption shall pretty give same xml.) :)
Comments
Post a Comment