@groovy.util.logging.Slf4j @groovy.transform.CompileStatic class JsonCommandServlet extends javax.servlet.http.HttpServlet
This class will accept JSON REST requests, find the named Spring Bean, find the method, and then invoke the method. Once complete, it will convert the return object to JSON and then send that back to the client. The requests are typically made from a Javascript client, although they could easily be made from Python, Java, or Objective C. The request comes in with http://yoursite.com/context/cmd/controllerName/methodName. The "cmd" part can be whatever name you map to this servlet in web.xml. The arguments are sent as the HTTP POST body, or they can be sent via query params like this:
http://yoursite.com/json/Controller/methodName?json=[arg1, arg2,...]
When calling the JsonServlet, it will always return an object in the form:
{"data":v,"status":false|true|null}
where the value 'v' is the return value of the Controller method called. The status is 'true' if the method call properly succeeded. Use the return value 'v' when status === true. If status === false then the communications reach the server, however, there was an exception. A Controller method threw an exception, invalid JSON was passed in, the name of the controller targeted was wrong, the controller targeted was not a BaseController, or the method on the controller was not found. The value 'v' when the the status === false will indicate the error. If the status === null then the call() method within the browser never reached the server. The value 'v' is a message indicating a network communications issue.
The returned JSON is gzip compressed if the caller indicates that it accepts Content-Encoding of gzip AND the return message is greater than 512 bytes. The return stream from methods that return large arrays and/or object graphs compresses especially well.
| Modifiers | Name | Description |
|---|---|---|
static java.lang.ThreadLocal<HttpServletRequest> |
servletRequest |
|
static java.lang.ThreadLocal<HttpServletResponse> |
servletResponse |
| Constructor and description |
|---|
JsonCommandServlet
() |
| Type Params | Return Type | Name and description |
|---|---|---|
|
protected void |
doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Handle JSON GET style request. |
|
protected void |
doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)Process JSON POST style where the controller, method, and arguments are passed in as the POST data. |
|
static java.lang.Throwable |
getDeepestException(java.lang.Throwable e)Get the deepest (original cause) of the exception chain. |
|
void |
init() |
|
void |
route(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)If using UrlRewrite from tuckey.org, then route HTTP Json commands via this 'route' method and list it in the urlrewrite.xml. |
| Methods inherited from class | Name |
|---|---|
class javax.servlet.http.HttpServlet |
javax.servlet.http.HttpServlet#service(javax.servlet.ServletRequest, javax.servlet.ServletResponse), javax.servlet.http.HttpServlet#getServletConfig(), javax.servlet.http.HttpServlet#getServletInfo(), javax.servlet.http.HttpServlet#getServletName(), javax.servlet.http.HttpServlet#getServletContext(), javax.servlet.http.HttpServlet#getInitParameter(java.lang.String), javax.servlet.http.HttpServlet#getInitParameterNames(), javax.servlet.http.HttpServlet#log(java.lang.String), javax.servlet.http.HttpServlet#log(java.lang.String, java.lang.Throwable), javax.servlet.http.HttpServlet#init(javax.servlet.ServletConfig), javax.servlet.http.HttpServlet#init(), javax.servlet.http.HttpServlet#destroy(), javax.servlet.http.HttpServlet#wait(long), javax.servlet.http.HttpServlet#wait(long, int), javax.servlet.http.HttpServlet#wait(), javax.servlet.http.HttpServlet#equals(java.lang.Object), javax.servlet.http.HttpServlet#toString(), javax.servlet.http.HttpServlet#hashCode(), javax.servlet.http.HttpServlet#getClass(), javax.servlet.http.HttpServlet#notify(), javax.servlet.http.HttpServlet#notifyAll() |
Handle JSON GET style request. In this case, 'controller', 'method', and 'json' are specified as URL parameters. Example: http://cedarsoftware.com/coolApp/CoolController/coolMethod?json=[args...]
Process JSON POST style where the controller, method, and arguments are passed in as the POST data. Example: http://cedarsoftware.com/coolApp/CoolController/coolMethod Post body contains the arguments in JSON format.
Get the deepest (original cause) of the exception chain.
e - Throwable exception that occurred.If using UrlRewrite from tuckey.org, then route HTTP Json commands via this 'route' method and list it in the urlrewrite.xml.