Introduction
Very simple usage
An instance of GroovyRestlet is the starting point of everything.
You can programmatically create an instance of GroovyRestlet:
Constructor method of #2 indicates that this instance of GroovyRestlet will consult Spring's ApplicationContext when constructing its component. So the Spring integration is done.
Alternatively, you can choose to declare GroovyRestlet in Spring bean definition. By this way, GroovyRestlet will automatically use its parent context when constructing its compoents.
Spring Integration
When Spring's ApplicationContext is provided, GroovyRestlet will try to consult Spring context for creating its components.
Two special attributes are provided for integrating GroovyRestlet and Spring.
- ofBean: to tell
GroovyRestletthat to create a component from Spring context by a bean name; - ofClass: to tell
GroovyRestletthat to create a component using Spring's AutowireCapableBeanFactory
|
|
Technical detail
GroovyRestlet
As the only entry point of GroovyRestlet, this instance automatically declares a number of global variables in the Groovy context. Users are also be able to provide their special context variables. In order to do that, users can provide a map of customer context when calling build method.
| Be careful This userDefinedContext is only visible for each call of build method. It will be merged into Groovy context and removed after each calling. And variables of userDefinedContext can override values of default context. Be careful when doing this. |
RestletBuilder
RestletBuilder is a simple implementation of Groovy's FactoryBuilderSupport. By default, all constructor factories are registered. Users are able choose which constructors are to be used freely.
To do this programmatically, just set a list of constructor factories after a build instance is created.
Alternatively, you can do this in Spring bean definition
Global variables
For convenience, GroovyRestlet add a number of Restlet-related variables to Groovy global context. Users can directly refer them in there building scrpits.
Name |
Usage |
Example |
|---|---|---|
builder |
The reference of a builder |
|
protocol |
Shortcut to Restlet Protocol |
|
mediaType |
Shortcut to Restlet MediaType |
|
status |
Shortcut to Restlet Status |
|
challengeScheme |
Shortcut to Restlet ChallengeScheme |
|
redirectorMode |
Shortcut to mode of Restlet Redirector |
|
routingMode |
Shortcut to mode of Restlet Router |
|
transformer |
Shortcut to mode of Restlet Transformer |
|
global |
Special shortcut providing a set of global variables and methods |
|
At current moment, you can call global.status(404) to create a CLIENT_ERROR_NOT_FOUND status.
Constructors
Every constructor is support ofBean and ofClass attributes. The value of ofBean must be a Sting. ofBean is only effected when a Spring context is provided. The value of ofClass can either be a String or an instance of Java Class.
restlet
The generic constructor for all components of type of Restlet. You can use ofClass attribute to indicate restlet to create an instance of a subtype. When doing that, using attribute consArgs to provide arguments array of corresponding constructor. For example: