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