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 hello-api.gradle :

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 objectMapper. The 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 Optional.

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 ControllerUnitTest
  • annotate the class with MicronautTest
  • replace the Gru definition with just @Inject Gru gru
  • assign the id of the loaded entities as we are using POGO implementation of Dru

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.

Advanced Migration

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

  1. Multiproject
  2. Configuration
  3. Static Compilation
  4. Datasets
  5. Marshalling
  6. Domain Classes
  7. Services
  8. Controllers
  9. Micronaut Application
  10. Micronaut Data

Sources & Discussion



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store