Grails Goodness: Pass JSON Configuration Via Command Line
Quick configuration of Grails applications using an environment variable loaded with JSON. This will let you easily change configuration with CD tools or scripts.
Join the DZone community and get the full member experience.
Join For FreeWe can use the environment variable SPRING_APPLICATION_JSON
with a JSON value as configuration source for our Grails 3 application. The JSON value is parsed and merged with the configuration. Instead of the environment variable we can also use the Java system property spring.application.json
.
Let's create a simple controller that reads the configuration property app.message
:
// File: grails-app/controllers/mrhaki/grails/config/SampleController.groovy
package mrhaki.grails.config
import org.springframework.beans.factory.annotation.Value
class MessageController {
@Value('${app.message}')
String message
def index() {
render message
}
}
Next we start Grails and set the environment variable SPRING_APPLICATION_JSON
with a value for app.message
:
$ SPRING_APPLICATION_JSON='{"app":{"message":"Grails 3 is Spring Boot on steroids"}}' grails run-app
| Running application...
Grails application running at http://localhost:8080 in environment: development
When we request the sample
endpoint we see the value of app.message
:
http -b :8080/message
Grails 3 is Spring Boot on steroids
$
If we want to use the Java system property spring.application.json
with the Grails command we must first configure the bootRun
task so all system properties are passed along:
// File: build.gradle
...
bootRun {
systemProperties System.properties
}
...
With the following command we pass the configuration as inline JSON:
$ grails -Dspring.application.json='{"app":{"message":"Grails 3 is Spring Boot on steroids"}}' run-app
| Running application...
Grails application running at http://localhost:8080 in environment: development
Written with Grails 3.1.8.
Published at DZone with permission of Hubert Klein Ikkink, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Trending
-
How To Backup and Restore a PostgreSQL Database
-
Batch Request Processing With API Gateway
-
Observability Architecture: Financial Payments Introduction
-
Execution Type Models in Node.js
Comments