Recently, I need to write to the app interface, usually return JSON format data, but the object to JSON always error, before the background interface using Ajax also appeared.
First, demand: Return student information, student Correspondence Department information, student corresponding class information JSON data
(1) Entity class
public class Student implements java.io.serializable{/** * */private static final long serialversionuid = 1l;private I Nteger id;private String Stunum;private string stuname;private string sex;private Timestamp birthday;private string Grade ;p rivate String photo;private Department department;private class_ class_;private role role;
(2) Find the Student object, call a method that you write to convert the object to JSON data and output
Student stufind=userfind.getstudent (); Jsonutil.tojson (Servletactioncontext.getresponse (), stufind);
(3) methods to convert to JSON and output
public class Jsonutil {//Use Gson--->xx to convert to JSON public static void ToJson (HttpServletResponse response, Object data ) throws IOException { Gson Gson = new Gson (); String result = Gson.tojson (data); Response.setcontenttype ("Text/json; Charset=utf-8 "); Response.setheader ("Cache-control", "No-cache"); Canceling the browser cache PrintWriter out = Response.getwriter (); SYSTEM.OUT.PRINTLN (result); Out.print (result); Out.flush (); Out.close (); }
(4) error message, stack Overflow, because students also associated with the Department, class and other information, it will continue to search according to the associated information, resulting in stack overflow or dead loop problems
Second, the solution
(1) Exclude students from role, department and role
Jsonconfig jc_student=new jsonconfig (); Jc_student.setjsonpropertyfilter (new PropertyFilter () {@Overridepublic Boolean apply (Object Class_, String property, Object Provalue) {if (Property.equals ("role") | | Property.equals ("department") | | Property.equals ("Class_")) {return true;} Else{return false;}}); Jsonarray json_student =jsonarray.fromobject (stufind,jc_student); Jsonutil.tojson (Servletactioncontext.getresponse (), json_student);
(2) Successful test results
Third, we also need to return to the students corresponding departments and classes, but not once the associated foreign key properties are removed by the above method some trouble
(1) Consider writing a common method to remove the management foreign key attribute of the corresponding object
Filter out the associated foreign key public static Jsonobject jsonfilter (Object obj, string[] filternames) { Jsonconfig jsonconfig = new Jsonconfig (); Jsonconfig.setignoredefaultexcludes (false); Jsonconfig.setcycledetectionstrategy (cycledetectionstrategy.lenient); Prevent self-contained if (filternames! = null) { //Here is the core, filter out the properties that you do not want to use jsonconfig. Setexcludes (Filternames); } Jsonobject jsonobj = Jsonobject.fromobject (obj, jsonconfig); return jsonobj; }
(2) Realize
Student stufind=userfind.getstudent (); String[] stuproperty={"role", "department", "Class_"};map.put ("Student", Jsonutil.jsonfilter (Stufind, Stuproperty)) ;//Department Department Deptfind=stufind.getdepartment (); String[] deptproperty={"Children", "parent", "students", "teachers", "classes", "Courses"};map.put ("department", Jsonutil.jsonfilter (Deptfind, Deptproperty));//Class Class_ Classfind=stufind.getclass_ (); String[] classproperty={"department", "Students"};map.put ("Class_", Jsonutil.jsonfilter (Classfind, Classproperty)) ; Jsonutil.tojson (Servletactioncontext.getresponse (), map);
(3) test success, students, departments, classes
Iv. Summary
It's always good to have a problem and learn more things. In short, there is a lot to learn, refueling.
Java EE SSH Framework object to JSON problem