Groovy service inputs and outputs
The signature of a Groovy function indicates expected service inputs and output.
Injectable arguments
When a service is made invokable via HTTP or used in a Martini endpoint, additional input variables can be injected for further use. All you need to do is declare the input variables needed by your service and ensure they are named accordingly. Martini won't inject arguments for parameters which do not follow expected parameter names.
Example injectable input variables are:
HttpServletRequest
object which contains the details of the HTTP request that triggered the service; andMartiniPackage
object which contains the details of the Martini package containing the service.
There are multiple other injectable arguments. In Groovy, they include the following:
- Arguments that can be injected to services called by Martini endpoints, listed and described in every endpoint type's respective page; and
- For APIs exposed via Groovy-based Spring controllers, supported
@RequestMapping
function arguments and return types are explained in the Spring documentation.
Injecting arguments in scripts
Since Groovy scripts don't have declared inputs or outputs, Martini will place all the available data in a
binding for you. It will execute your Groovy script with the binding, giving it access to
all variables. Below is a simple piece of code that logs the HTTP request object to the Martini
logger at the INFO
level:
1 |
|
Injecting arguments in functions
Unlike Groovy scripts, Groovy functions declare their input variables. To do something similar to the Groovy script example earlier, declare the variable as a parameter. For example, the function below publishes the string to a JMS queue.
1 2 3 |
|