# json-command-servlet **Repository Path**: mirrors_jdereg/json-command-servlet ## Basic Information - **Project Name**: json-command-servlet - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2020-08-09 - **Last Updated**: 2026-03-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README json-command-servlet ==================== [![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.cedarsoftware/json-command-servlet/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.cedarsoftware/json-command-servlet) [![Javadocs](http://www.javadoc.io/badge/com.cedarsoftware/json-command-servlet.svg?color=brightgreen)](http://www.javadoc.io/doc/com.cedarsoftware/json-command-servlet) Java servlet that processes REST requests and returns JSON responses. Built with JDK 17. Available on [Maven Central](https://central.sonatype.com/search?q=json-command-servlet&namespace=com.cedarsoftware). To include in your project: ##### Gradle ``` implementation 'com.cedarsoftware:json-command-servlet:2.2.0' ``` To include in your project: ``` com.cedarsoftware json-command-servlet 2.2.0 ``` REST calls can be sent as HTTP GET or POST requests. To make a POST request, it should be formatted like this: HTTP://mycompany.com/Context/controller/method [HTTP HEADERS] CR LF CR LF [arguments] where: 'controller' is the bean name of a controller that exposes methods that can be publicly called. 'method' is the name of the method to be called on the controller. [arguments] is a JSON array of arguments matching the arguments required by the method. The method will be called, and then the return value from the method will be written in JSON format, like this: {"status":true,"data": [result value in JSON]} # Result not in array brackets unless result is array where 'status' is true if the method called returned without exception, otherwise 'status' is false. The 'result' is the JSON string-ified version of the method's return value. If the method throws an exception, the exception message is the "result" and the status is false, and there will be an `exception` field in the response envelope. See *json-io* for a Java-script `call()` method that makes the REST request for you with a very simple notation. Use `call("FooController.barMethod", [])` In order for a `Controller` class (That is the standard name of classes that are callable externally) to be called, it must have the Spring annotation `@RestController`. This can be added directly to the class, or to an interface that the class inherits from. By default, marking a class as `@RestController`, all public methods are available to be called externally (through this servlet). If you have a public method that you do not want called, place the Java annotation `@ControllerMethod` on the method with `allow=false` `[@ControllerMethod(allow = false)]`. The `JsonCommandServlet` will honor this annotation and return a JSON error message indicating that a method cannot be accessed remotely via this command servlet. No special annotations have to be added to your methods to be called externally. Simply add the `@RestController` annotation to the Controller or its interface, and the public methods will be accessible. This effectively makes all Controller method's like mini-servlets. If you need access to the `HttpServletRequest` or `HttpServletResponse` objects within your Controller method, you can access `JsonCommandServlet.servletRequest.get()` or `JsonCommandServlet.servletResponse.get()`. The command servlet places the request and response objects on a `ThreadLocal` and makes it available for the lifespace of your method call. If you need to stream data back (instead of having a normal return value from a controller method), you can use the `JsonCommandServlet.servletResponse.get()` to obtain the `HttpServletResponse` object, and then use the response object set HTTP Response headers and to get the `OutputStream` to write to. You may be wondering, how do I prevent the `JsonCommandServlet` from writing a JSON standard response object instead of the streamed data? `JsonCommandServlet` will detect that the output stream has been written to, and will not write the standard return (JSON) envelope. Setup ===== Configure `JsonCommandServlet` using either `web.xml` or `UrlRewrite.xml` (tuckey.org). Example `urlrewrite.xml` configuration: Redirect inbound requests using n-cube's sys.classpath ^/sys/[^/]+/.*$ sys null Redirect inbound requests using n-cube's sys.classpath ^/cmd/[^/]+/.*$ null See the [jsonUtil.js](https://github.com/jdereg/json-io/blob/master/jsonUtil.js) file that ships with **json-io** for an easy way to make REST calls from Javascript. See [changelog.md](/changelog.md) for revision history. --- ### Sponsors [![Alt text](https://www.yourkit.com/images/yklogo.png "YourKit")](https://www.yourkit.com/.net/profiler/index.jsp) YourKit supports open source projects with its full-featured Java Profiler. YourKit, LLC is the creator of YourKit Java Profiler and YourKit .NET Profiler, innovative and intelligent tools for profiling Java and .NET applications. Intellij IDEA from JetBrains **Intellij IDEA** --- By: John DeRegnaucourt