Converts the results of a query in a database to JSON, and then calls the interface back to JSON

Source: Internet
Author: User

The calling interface, which is nothing more than opening the link read stream, outputs the result as a stream

Returns the query results in JSON, nothing more than converting the results of the query into Jsonobject

=============================================================

/***
     * Returns the results from the data in JSON format
     *
     * @param response
     * @throws ioexception
     */
     @RequestMapping (value = " /getjson ")
    public void Getjson (HttpServletResponse response) throws IOException {
         list List = Userservice.getusers ();
        jsonarray Js = jsonarray.fromobject (list);
        jsonobject jsonobject = new Jsonobject ();
        jsonobject.put ("MSG", "success");
        jsonobject.put ("Data", js.tostring ());
        servletoutputstream out = Response.getoutputstream ();
        out.print (jsonobject.tostring ());
    }

=============================================================

/***
* Call Interface
* @param mm
* @param response
* @return
* @throws IOException
*/
@RequestMapping (value = "/getuser")
Public String getusers (modelmap mm, httpservletresponse response)
Throws IOException {
String url = "Http://127.0.0.1:8080/ssm/getJson";
URL posturl = new URL (URL);
HttpURLConnection connection = (httpurlconnection) posturl.openconnection ();
Connection.setdooutput (TRUE);
Connection.setdoinput (TRUE);
Connection.setrequestmethod ("POST");
Connection.setusecaches (FALSE);
Connection.setinstancefollowredirects (TRUE);
Connection.setrequestproperty ("Content-type",
"application/x-www-form-urlencoded");
Map<string,string> headermap=new hashmap<string,string> ();
if (!headermap.isempty ()) {
For (map.entry<string, string> entry:headerMap.entrySet ()) {
Connection.setrequestproperty (Entry.getkey (), Entry.getvalue ());
}
}
Connection.connect ();
DataOutputStream out = new DataOutputStream (Connection.getoutputstream ());
Out.writebytes (content);//content as method parameter
Out.flush ();
Out.close (); Flush and close
BufferedReader reader = new BufferedReader (New InputStreamReader (Connection.getinputstream (), "Utf-8"));
String line= "";
line = Reader.readline ();
Reader.close ();
Connection.disconnect ();
Mm.addattribute ("line", line);
Mm.addattribute ("List", Userservice.getusers ());
return "index.jsp";
}

Converts the results of a query in a database to JSON, and then calls the interface back to JSON

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.