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 →
In Spring we use the @EnableAutoConfiguration
each time when we use the @SpringBootApplication
annotation.
If we look at the @SpringBootApplication
we can see that this automatically enables the @EnableAutoConfiguration
.
This last mentioned annotation triggers all the auto-configuration enabled configurations on the classpath.
We can write an auto-configuration enabled @Configuration
ourself in only two steps.
package com.jdriven.example;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyOwnAutoConfiguration {
//You can define your own beans here and
//further setup this Configuration as you normally would do
}
Continue reading →
In Spring MVC we get some method argument types resolved by default and injected in Spring MVC controller methods.
Some examples are Model
, Locale
and OutputStream
.
What if we want to inject a custom argument in Spring MVC controller methods?
In this example we extract the X-Application-Version
HTTP header from the request and inject that as a method argument called version
.
Our controller class will look like the following:
@RestController
public class MyController {
@RequestMapping("/persons")
//I want the version to be automatically injected
public List getPersons(String version) {
.....
return someList;
}
}
Continue reading →