Ajax request JSON Data exception: Nested exception is net.sf.json.JSONException:java.lang.reflect.InvocationTargetException] with root Cause

Source: Internet
Author: User
Tags string format

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.