Goodbye Grails, Hello Micronaut #8: Controllers
This is the eighth post in a series that will guide you through the migration from Grails to Micronaut. This guide requires your application to be based on Grails 4.x or later.
We have migrated the services into a separate library in the last step. In this part, we are going to migrate Grails controllers into their Micronaut counterparts.
Let's add declarative error handling into our existing Grails controller first.
The test should be still passing.
Let's create a new library
hello-api in the
libs directory with a build file similar to the following
The build file contains all we need to create Micronaut controllers and test them. It is quite easy to rewrite the controllers after we have already switched to Jackson marshalling. The major difference is that we need to apply the HTTP mapping annotations.
The injection now happens in the constructor. As Micronaut is using Jackson for the marshalling, we remove the explicit usage of
VehicleReponse class can be copied from the existing Grails application as it is. Micronaut is returning
404 Not Found for empty
Optional so we can take advantage of that the repository interface now returns
There are only minor changes required in the specification. You can copy the original one from the Grails application and do the following changes:
- remove all the traits such as
- annotate the class with
- replace the
Grudefinition with just
@Inject Gru gru
- assign the
idof the loaded entities as we are using POGO implementation of
We also need to copy
MockDataServiceFactory into the
hello-api project. The fixture files such
vehicle.json should be copied from the Grails applications
src/test/resources folders to ensure the returned JSON remains unchanged.
Migrating controllers themselves is relatively easy. You may face another there are still some parts missing e.g. HTTP filters, Grails Security. You will have to find their counterparts in the Micronaut Documentation:
In the next part, we are going to create the new Micronaut application.
Table of Contents
- Static Compilation
- Domain Classes
- Micronaut Application
- Micronaut Data