Ajax request JSON Data exception: Nested exception is net.sf.json.JSONException:java.lang.reflect.InvocationTargetException] with root Cause1. Exception reason: The requested JSON data contains the Java.util.date data type, but it is not formatted in the background 2. Workaround: Add Tool class Datejsonvalueprocessor
ImportJava.text.SimpleDateFormat;ImportNet.sf.json.JsonConfig;ImportNet.sf.json.processors.JsonValueProcessor; Public classDatejsonvalueprocessorImplementsjsonvalueprocessor{PrivateString format; Publicdatejsonvalueprocessor (String format) { This. Format =format; } PublicObject Processarrayvalue (object value, Jsonconfig jsonconfig) {return NULL; } PublicObject Processobjectvalue (String key, object value, Jsonconfig jsonconfig) {if(Value = =NULL) { return""; } if(Valueinstanceofjava.sql.Timestamp) {String str=NewSimpleDateFormat (format). Format ((java.sql.Timestamp) value); returnstr; } if(Valueinstanceofjava.util.Date) {String str=NewSimpleDateFormat (format). Format ((java.util.Date) value); returnstr; } returnvalue.tostring (); }}
3. Then write to your controller
jsonconfig cfg = new jsonconfig (); Cfg.registerjsonvalueprocessor (java.util.date.class,new datejsonvalueprocessor ("Yyyy-MM-dd" )); Jsonarray json = jsonarray.fromobject (users, CFG);
@RequestMapping ("/getalljsonuser") publicvoid Getalljsonuser ( HttpServletResponse response) { List<User> users = userservice.listall (); System.out.println (users); New jsonconfig (); Cfg.registerjsonvalueprocessor (java.util.Date. class,new datejsonvalueprocessor ("Yyyy-mm-dd")); = jsonarray.fromobject (users, CFG); Jsonutils.ajaxjson (json.tostring (), response); }
OK, problem solving
Ajax Request JSON data exception: Nested exception is net.sf.json.JSONException:java.lang.reflect.InvocationTargetException] with Root cause