The default JSON error body of Spring Boot can be customized or extended by defining our own ErrorAttributes
implementation.
In Spring Boot we get an error response JSON format for free.
Any thrown Exception is automatically translated to this JSON format and returned with a corresponding HTTP status.
As soon as we throw an Exception in a @RequestMapping
annotated method of a @Controller
, the thrown Exception is translated to a HTTP status and a JSON error body.
The default JSON error body looks like:
Continue reading →
Groovy has many AST annotations that add code to our class (the Abstract Syntax Tree - AST) before it is compiled.
So the compiled class file contains the code added by the AST annotation.
With the @AutoClone
annotation a clone
method is added and the class implements the Cloneable
interface.
We have different strategies to choose from to support cloning for our class.
The default strategy is to invoke super.clone()
in the generated clone
method.
The next statements will deep copy the properties (and optional fields) from our class.
If one of the properties cannot be cloned an exception is thrown.
In the following example code snippet we apply the @AutoClone
annotation to the classes Course
and Teacher
:
Continue reading →
In a previous post we learned how to add a banner to a Grails 3.0 application.
We used the Spring Boot support in Grails to show a banner on startup.
The solution we used doesn't work for a Grails 3.1 application.
We need to implement a different solution to show a banner on startup.
First of all we create a new class that implements the org.springframework.boot.Banner
interface.
We implement the single method printBanner
and logic to display a banner, including colors:
Continue reading →
With a Spring (Boot/Cloud) application you can create a fully executable JAR, where the jar can be run from the command-line by just calling ./my-own-jar-0.0.1.jar
.
My colleague Mr. Haki wrote Grails Goodness: Creating A Fully Executable Jar.
Together with the famous one-liner of Josh Long in mind: "Make JAR not WAR!", create a JAR whenever you develop a Spring Boot/Cloud application.
As described in Mr. Haki's blog, it is very easy to make our Spring application executable:
org.springframework.boot
spring-boot-maven-plugin
true
## Gradle configuration
```gradle
apply plugin: 'spring-boot'
springBoot {
executable = true
}
Continue reading →
With Grails 3 we can create a so-called fat jar or war file.
To run our application we only have to use java -jar
followed by our archive file name and the application starts.
Another option is to create a fully executable jar or war file, which adds a shell script in front of the jar or war file so we can immediately run the jar or war file.
We don't have to use java -jar
anymore to run our Grails application.
The fully executable JAR file can only run on Unix-like systems and it is ready to be used as service using init.d
or systemd
.
To create a fully executable jar file for our Grails application we must add the following lines to our build.gradle
file:
Continue reading →
In a previous post we learned how to add Git commit information to the /info
endpoint in our Grails application.
We can add our own custom information to this endpoint by defining application properties that start with info.
.
Let's add the Grails environment the application runs in to the /info
endpoint.
We create the file grails-app/conf/application.groovy
.
To get the value we must have a piece of code that is executed so using the application.groovy
makes this possible instead of a static configuration file like application.yml
:
Continue reading →
I love code. I take care of my code and I like my code to be formatted nicely.
No matter if I'm on Eclipse, Netbeans or IntelliJ, I want my code to be formatted the same.
Nowadays we have EditorConfig.
In the source code we can place a file .editorconfig
with formatting instructions.
These instructions can be read by many Tools like Eclipse, Netbeans, IntelliJ and VisualStudio.
If we create an .editorconfig
file with formatting instructions, these rules are automatically applied.
An example of an .editorconfig
file looks like:
Continue reading →
We know Grails 3 is based on Spring Boot.
This means we can use Spring Boot features in our Grails application.
For example a default Grails application has a dependency on Spring Boot Actuator, which means we have a /info
endpoint when we start the application.
We add the Git commit id and branch to the /info
endpoint so we can see which Git commit was used to create the running application.
First we must add the Gradle Git properties plugin to our build.gradle
file.
This plugin create a git.properties
file that is picked up by Spring Boot Actuator so it can be shown to the user:
Continue reading →
Ratpack has a very useful class: TestHttpClient
.
This is a blocking HTTP client that we normally use for testing our Ratpack applications.
For example we use MainClassApplicationUnderTest
or GroovyRatpackMainApplicationUnderTest
in a test and invoke the getHttpClient
method to get an instance of TestHttpClient
.
The class has a lot of useful methods to make HTTP requests with a nice DSL.
TestHttpClient
is also very useful as a standalone HTTP client in other applications.
Suppose we have a piece of code that needs to access MapQuest Open Platform Web Services to get location details for a given combination of longitude and latitude values.
In the constructor we create an instance of the interface ApplicationUnderTest
.
We then can use the getHttpClient
method of ApplicationUnderTest
to get a TestHttpClient
instance:
Continue reading →
When we write code with a lot of method chaining that involves closures and the use the Reformat code feature of IntelliJ IDEA things might get screwed up.
Luckily we can changes some Groovy formatting rules in IntelliJ IDEA to prevent the reformatting.
In the following screenshot we see the original piece of code in the IntelliJ IDEA editor:
Continue reading →
Suppose we have a piece of code that uses an external HTTP service.
If we write a test for this code we can invoke the real HTTP service each time we execute the tests.
But it might be there is a request limit for the service or the service is not always available when we run the test.
With Ratpack it is very, very easy to write a HTTP service that mimics the API of the external HTTP service.
The Ratpack server is started locally in the context of the test and we can write extensive tests for our code that uses the HTTP service.
We achieve this using the Ratpack EmbeddedApp
or GroovyEmbeddedApp
class.
With very little code we configure a server that can be started and respond to HTTP requests.
In our example project we have a class GeocodeService
that uses the external service MapQuest Open Platform Web Services.
We use the HTTP Requests library to make a HTTP request and transform the response to an object:
Continue reading →
When we define our Ratpack application using the Groovy DSL in a file ratpack.groovy
, we can split up the definition in multiple files.
With the include
method inside the ratpack
configuration closure we can use the file name of the file we want to include.
The file that we include also contains a ratpack
configuration closure.
We can use the same bindings
, handlers
and serverConfig
sections.
The bindings
configuration is appended to the parent configuration.
The handlers
and serverConfig
configuration is merged with the parent configuration.
In an example project we have the following ratpack.groovy
, that includes two extra files: course.groovy
and loghandler.groovy
:
Continue reading →