In Grails we can use the @Resource
annotation to make a domain class a REST resource.
The annotation adds a controller as URL endpoint for the domain class.
Values for the domain class properties are rendered with a default renderer.
We can use JSON and markup views to customize the rendering of the domain class annotated with a @Resource
annotation.
First we must make sure we include views plugin in our build configuration.
Then we must create a directory in the grails-app/views
directory with the same name as our domain class name.
Inside the directory we can add JSON and markup views with names that correspond with the controller actions.
For example a file index.gson
or index.gml
for the index
action.
We can also create a template view that is automatically used for a resource instance by adding a view with the name of the domain class prefixed with an underscore (_
).
In the next example application we create a custom view for the Book
domain class that is annotated with the @Resource
annotation:
Continue reading →
Normally when we create a domain class in Grails we rely on GORM for all the persistence logic.
But we can use the static property mapWith
with the value none
to instruct Grails the domain class is not persisted.
This can be useful for example if we want to use a RestfulController
for a resource and use the default data binding support in the RestfulController
.
The resource must be a domain class to make it work, but we might have a custom persistence implementation that is not GORM.
By using the mapWith
property we can still have benefits from the RestfulController
and implement our own persistence mechanism.
In the following example we have a simple Book
resource.
We define it as a domain class, but tell Grails the persistence should not be handled by GORM:
Continue reading →
In Groovy we can add a method named call
to a class and then invoke the method without using the name call
.
We would simply just type the parentheses and optional arguments on an object instance.
Groovy calls this the call operator: ()
.
This can be especially useful in for example a DSL written with Groovy.
We can add multiple call
methods to our class each with different arguments.
The correct method is invoked at runtime based on the arguments.
In the following example we have User
class with three call
method implementations.
Next we see how we invoke the call
methods, but without typing the method name and just use the parenthesis and arguments:
Continue reading →
To create JSON output with Groovy is easy using JsonBuilder and StreamingJsonBuilder.
In the samples mentioned in the links we create a JSON object with a key and values.
But what if we want to create JSON with a root JSON array using JsonBuilder
or StreamingJsonBuilder
? It turns out to be very simple by passing a list of values using the constructor or using the implicit method call
.
In the following example we use JsonBuilder
to create a root JSON array:
Continue reading →
Sometimes we want to check which operating system is used in our build script.
For example we have tasks that need to run if the operating system is Windows and not for other operating systems.
Gradle has an internal class org.gradle.nativeplatform.platform.internal.DefaultOperatingSystem
, but we should not use this class in our build scripts.
The class is used internally by Gradle and can change without warning.
If we would depend on this class and it changes we break our build scripts.
But we can use a class from Ant that is already in Gradle’s class path: org.apache.tools.ant.taskdefs.condition.Os
.
The class has several methods and constants to check the operating system name, version and architecture.
The values are based on the Java system properties os.name
, os.version
and os.arch
.
In the following example build script we use import static
to include the Os
class, so we can directly invoke the methods and refer to the constants in the Os
class.
We add some tasks that have a condition check with onlyIf
so the task only runs when the condition in the closure is true
.
The task osInfo
simply shows values from the Os
class:
Continue reading →
Since Groovy 2.4.8 we can use the uncapitalize
method on CharSequence
objects.
The capitalize
method was already available for a long time, but now we have the opposite as well.
In the following example we see that the uncapitalize
method only replaces the first letter of a String
value to lower case:
Continue reading →
Adding a block title in Asciidoctor is easily done by adding a line at the top of the block that starts with a dot (.
).
The text following the dot is then used as the title of the block.
But if the text of the title itself starts with a dot (.
) Asciidoctor get’s confused.
For example if we want to use a filename that starts with a dot (.filename
) we must use different syntax to set the block title with the filename.
In the next Ascciidoc markup sample we use different ways to set the block title for a code block with a filename that starts with a dot.
First we use the title
attribute for the block.
Another solution is to use the Unicode value for the dot.
Next we enclose the filename in back ticks ( `
) which also formats the filename with a monotype font.
And finally we can define the filename via a document attribute and reference the document attribute in the block title:
Continue reading →
Gradle builds are fast because Gradle supports incremental tasks.
This means Gradle can determine if input or output of task has changed, before running the task.
If nothing has changed a task is marked a up-to-date and the task is not executed, otherwise the task is executed.
If we want execute a task even if it is up-to-date we must use the command line option --rerun-tasks
.
In the following example we run the assemble
task for a simple Java project, and we see all tasks are executed.
When we invoke the assemble
task again we see the tasks are all up-to-date:
Continue reading →
With Spring Boot Actuator we get some useful endpoints in our application to check on our application when it is running.
One of the endpoints is the /info
endpoint.
We can add information about our application if Spring Boot finds a file META-INF/build-info.properties
in the classpath of our application.
With the Gradle Spring Boot plugin we can generate the build-info.properties
file.
When we apply the Gradle Spring Boot plugin to our project we get a Gradle extension springBoot
in our build file.
With this extension we can configure Spring Boot for our project.
To generate project information that is used by the /info
endpoint we must add the method statement buildInfo()
inside the springBoot
extension.
With this method statement the Gradle Spring Boot plugin generates a file build/main/resources/META-INF/build-info.properties.
.
Let’s run our application and send a request for /info
:
Continue reading →
With Spring Boot Actuator we get some endpoints that display information about our application.
One of the endpoints is the /info
endpoint.
If our project uses Git we can add information about Git to the /info
endpoint.
By default Spring Boot will look for a file git.properties
in the classpath of our application.
The file is a Java properties file with keys that start with git.
and have values like the branch name, commit identifier and commit message.
Spring Boot uses this information and when we request the /info
endpoint we get a response with the information.
This can be very useful to check the Git information that was used to build the application.
To create the git.properties
file we can use a Gradle (or Maven) plugin that will do the work for us.
In the following example we use the Gradle plugin to generate the git.properties
file for our project.
The Gradle Git properties plugin is added in the plugins
configuration block.
The plugin adds a Gradle extension gitProperties
that can be used to customize the output in git.properties
.
We could even change the location, but we keep it to the default location which is build/resources/main/git.properties
.
Continue reading →
When we generate a PNG version of our PlantUML definition the original definition is stored in the PNG image.
We can extract the definition using the command line option -metadata
.
We need to provide the PNG file and in the output we see the original PlantUML definition.
The following PNG image (activity.png
) is created with PlantUML:
Continue reading →
We can change the line style and color when we "draw" the line in our PlantUML definition.
We must set the line style and color between square brackets ([]
).
We can choose the following line styles: bold
, plain
, dotted
and dashed
.
The color is either a color name or a hexadecimal RGB code prefixed with a hash (#
).
In the following example activity diagram we apply different styles and colors to the lines:
Continue reading →