Grails 3 is based on Spring Boot. This means we get a lot of the functionality of Spring Boot into our Grails applications. A Spring Boot application has by default a banner that is shown when the application starts. The default Grails application overrides Spring Boot's behavior and disables the display of a banner. To add a banner again to our Grails application we have different options.
First we can add a file banner.txt
to our classpath. If Grails finds the file it will display the contents when we start the application. Let's add a simple banner with Grails3 in Ascii art in the file src/main/resources/banner.txt
. By placing the file in src/main/resources
we can assure it is in the classpath as classpath:/banner.txt
:
Continue reading →
Since the latest Grails versions a Grails wrapper is automatically created when we execute the create-app
command. If we don't want the wrapper to be created we can use the command argument --skip-wrapper
. If later we changed our mind and want the Grails wrapper we can simply run the wrapper
command from our Grails application directory.
Let's run the create-app
command with the --skip-wrapper
argument. If we check the contents of the created directory we see that the wrapper files are not created:
Continue reading →
Since version 2.3, Grails supports asynchronous parallel programming to support modern multiple core hardware. Therefore a new Grails command is added to generate asynchronous controllers for domain classes. The generated controller contains CRUD actions for a given domain class. In the example below, we will generate a default asynchronous implementation of a Grails controller. First we create a domain object:
$ grails create-domain-class grails.data.Movie
Continue reading →
Bintray JCenter is the next generation (Maven) repository. The repository is already a superset of Maven Central, so it can be used as a drop-in replacement for Maven Central. To use Bintray JCenter we only use jcenter()
in our repositories
configuration block in BuildConfig.groovy
. This is the same as we would use in a Gradle build.
// File: grails-app/conf/BuildConfig.groovy
...
repositories {
...
// Configure Bintray JCenter as repo.
jcenter()
...
}
...
Continue reading →
In a previous post we saw that we can use Spring's Java configuration feature to load beans in our Grails application. We can use the @Profile
annotation to conditionally load beans based on the currently active Spring profile. We can for example use the Java system property spring.profiles.active
when we start the Grails application to make a profile active. But wouldn't it be nice if we could use the Grails environment setting to conditionally load beans from a Java configuration? As it turns out Grails already set the current Grails environment name as the active profile for the Grails application. So we can simply use @Profile
in our configuration or component classes, like in a non-Grails Spring application.
We can use the @Profile
annotation in a Spring Java configuration class. The following sample shows different values for the @Profile
annotation. The annotation is applied to methods in the sample code, but can also be applied to a class.
Continue reading →
A Grails application uses Spring under the hood, which means we can also use all of Spring's features in a Grails application. For example we can use the Spring Java configuration feature with our Grails application. The Java configuration feature allows us to write a Java or Groovy class to define beans for our application. Of course in Grails we have the nice bean builder syntax when we define beans in the grails-app/conf/spring/resources.groovy
file, but maybe we must include a Java configuration from an external library or we just want to write a configuration class ourselves.
This post is very much inspired by this blog post by Andres Steingress.
Continue reading →
Sometimes we want to support in our RESTful API a different level of detail in the output for the same resource. For example a default output with the basic fields and a more detailed output with all fields for a resource. The client of our API can then choose if the default or detailed output is needed. One of the ways to implement this in Grails is using converter named configurations.
Grails converters, like JSON
and XML
, support named configurations. First we need to register a named configuration with the converter. Then we can invoke the use
method of the converter with the name of the configuration and a closure with statements to generate output. The code in the closure is executed in the context of the named configuration.
Continue reading →
In Grails we can apply the @Resource
AST (Abstract Syntax Tree) annotation to a domain class. Grails will generate a complete new controller which by default extends grails.rest.RestfulController
. We can use our own controller class that will be extended by the @Resource
transformation. For example we might want to disable the delete
action, but still want to use the @Resource
transformation. We simply write a new RestfulController
implementation and use the superClass
attribute for the annotation to assign our custom controller as the value.
First we write a new RestfulController
and we override the delete
action. We return a HTTP status code 405 Method not allowed:
Continue reading →
We can write a RESTful application with Grails and define our API in different ways. One of them is to subclass the grails.rest.RestfulController
. The RestfulController
already contains a lot of useful methods to work with resources. For example all CRUD methods (save/show/update/delete
) are available and are mapped to the correct HTTP verbs using a URL mapping with the resource(s)
attribute.
We can define which content types are supported with the static variable responseFormats
in our controller. The variable should be a list of String
values with the supported formats. The list of supported formats applies to all methods in our controller. The names of the formats are defined in the configuration property grails.mime.types
. We can also use a Map
notation with the supportedFormats
variable. The key of the map is the method name and the value is a list of formats.
Continue reading →
Especially when we use Grails to create a RESTful API we want to enable the request header Accept
, so Grails can do content negotiation based on the value in the header. For example we could use the request header Accept: application/json
to get a JSON response. Grails will look at the boolean
configuration property grails.mime.use.accept.header
to see if the Accept
header must be parsed. The default value is true
so the Accept
header is used. But there is another property which determines if the Accept
header is used: grails.mime.disable.accept.header.userAgents
. The value must contain a list or a regular expression pattern of user agents which are ignored for the Accept
header. The default value is ~/(Gecko(?i)|WebKit(?i)|Presto(?i)|Trident(?i))/
. So for any request from these user agents, mostly our web browser, the Accept
header is ignored.
If we use REST web services test tools from our browser, for example Postman, then any Accept
header we set in the tool is ignored by Grails. We must change the value of the configuration property grails.mime.disable.accept.header.userAgents
to for example an empty list. Then we know for every request send to our Grails application, either from a web browser or command-line tool like curl, the Accept
header is interpreted by Grails.
Continue reading →
We can use the run-script
command to run Groovy scripts within the context of a Grails application. We can pass one or more Groovy scripts as argument to the run-script
command. The Grails environment will be configured and we can access the Spring application context, domain classes, Grails services and more. Basically everything we can do in the Grails console or shell can be saved as a Groovy script and run with the run-script
command.
The following Groovy script shows some stats for a Grails application:
Continue reading →
In Grails we can add aliases for standard Grails commands with the alias
command. For example we want to use another name for a command or combine a command with arguments to a single alias. With the following command we create a start-app
alias for the standard run-app
command:
$ grails alias start-app run-app
Alias start-app with value run-app configured
$
Continue reading →