"Homemade Tool class" struts returns the JSON data wrapper format class

Source: Internet
Author: User
Tags tojson

wrote to struts back to the JSON data wrapper format class, do not like to spray, original, need to introduce Com.alibaba.fastjson jar package in the project

First look at the effect (here is not using MSG, interested in looking down):

On Demo:

1 ImportJava.util.HashMap;2 3 4 /**5 * JSON response processing tool class6 * 7 * Returns a JSON object that contains Status/msg/data,8 * Checkok () default status:200/checkfail () default status:5009 * where Checkok (object data) is very special, the JSON of this object is returned directlyTen * All others are status/msg/data return format One *  A * @authorHellxz - * @date October 21, 2017 - * @version1.0 the */ -  Public classJsonresult { -  -     /**check success, return 200**/ +      Public StaticObject Checkok () { -Object result = Build (200,NULL,NULL); +         returnresult; A     } at     /**detection Successful, return 200,msg**/ -      Public StaticObject Checkok (String msg) { -Object result = Build ($, MSG,NULL); -         returnresult; -     } -     /**check success, return JSON object directly **/ in      Public StaticObject Checkok (Object data) { -Object result =Com.alibaba.fastjson.JSONObject.toJSON (data); to         returnresult; +     } -      the     /**Check for successful response, return 200,msg,data**/ *      Public StaticObject Checkok (String msg, object data) { $Object result = Build (200, MSG, data);Panax Notoginseng         returnresult; -     } the      +     /**check failed, return only 500**/ A      Public StaticObject Checkfail () { theObject result = Build (500,NULL,NULL); +         returnresult; -     } $      Public StaticObject Checkfail (String msg) { $Object result = Build ($, MSG,NULL); -         returnresult; -     } the      -     /**Check for failed responses 500,msg,data**/Wuyi      Public StaticObject checkfail (String msg,object data) { theObject result = Build (500, MSG, data); -         returnresult; Wu     } -      About     /**returns the converted JSON object (custom status code, information, data) **/ $      Public StaticObject Build (Integer status, String msg, object data) { -hashmap<string,object> map =NewHashmap<>(); -Map.put ("Status", status); -Map.put ("MSG", msg); AMap.put ("Data", data); +Object JSON =Com.alibaba.fastjson.JSONObject.toJSON (map); the         returnJSON; -     } $      the}

Because this is a tool class, all methods are static methods, so, write out the output stream needs to write manually, the bottom of the simple write a need to put in the action class of small method, with the above tool class edible flavor better ~!

 /**   */ public   void   Writeout (Object obj) {response.setcontenttype ( "text/json;charset=        Utf-8 ");        PrintWriter writer  = null  ;  try   {writer  = Response.getwriter ();        Writer.println (obj);  catch   (IOException e) {e.printstackt        Race ();             finally  {writer.flush ();        Writer.close (); }    }

So in action only need to introduce the most mentioned tool class, and then write back to the client with this method, so simple ~

// Write Success writeout (Jsonresult.checkok (); Writeout (Jsonresult.checkok ("output succeeded"); Writeout (Jsonresult.checkok ( "Output succeeded"/ / This is the only different format // write failure writeout (Jsonresult.checkfail (); Writeout (Jsonresult.checkfail ("Output failed"); Writeout (Jsonresult.checkfail ("Output failed", Object o); // Custom writeout (Jsonresult.build (400, "Hello", Object o);

If there's a better code welcome to the communication

Self-made tool class struts returns the JSON data wrapper format class

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.