Backward compatible Grails 2.5

Earlier this year we migrated our main web application from Grails 1.3.7 to a more recent version 2.5.0.

Generally the migration went through without major problems and the main work that had to be done was changing the setup of our Spock unit tests due to the way domain mocking has changed between Grails 1.x and 2.x. For further information on this specific case I highly recommend this article.

But when we started the manual tests on the upgraded application in our staging environment we ran into an issue with the way the links were being generated. As a Grails user you might be aware of the ApplicationTagLib which relies on the LinkGenerator interface to create absolute and relative links within your web application.

The problem is that the use of the config variable grails.serverUrl has changed as of Grails 2.x.

The definition is as follows:

grails.serverURL – A string specifying the server URL portion of absolute links, including server name e.g. grails.serverURL=””. See createLink. Also used by redirects.

If you look closely you’ll spot the added note of “Also used by redirects”, which is mentioned as part of the upgrade notes for previous versions of Grails as well.

The redirect() method now uses the grails.serverURL config setting to generate the redirect URL. You may need to remove the setting, particularly from the development and test environments.

For most people this won’t be a big deal as you could just follow the recommendation from the documentation and remove the config for dev and test environments. However for us this created a widespread issue in the way we were creating links and redirects throughout the various places in the application and different staging environments.

We discussed a couple of different potential solutions like: refactoring all places where we made use of LinkGenerator or patching the internal implementations of LinkGenerator but none of those were maintainable solutions.

Until we came across a much cleaner solution which is possible due to how the Spring framework (and to it’s extend Grails) works with beans.

By default the LinkGenerator is initialised as a bean called grailsLinkGenerator during application startup. So in order to overwrite the behaviour of it we just had to change the bean definition in grails-app/conf/spring/resources.groovy to use our own version of the LinkGenerator implementation.

beans { grailsLinkGenerator(SELinkGenerator) {} }

In order to have the same behaviour of the LinkGenerator + serverUrl configuration as it previously was in Grails 1.3.7 we changed the implementation by extending the DefaultLinkGenerator:

import grails.util.Holders
import org.codehaus.groovy.grails.web.mapping.DefaultLinkGenerator
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest

class SELinkGenerator extends DefaultLinkGenerator {

   public SELinkGenerator() {

   String makeServerURL() {
      final webRequest = GrailsWebRequest.lookup()
      def u = webRequest?.baseUrl

      if(!u || u ==~ /.*localhost$/) {
         u = configuredServerBaseURL

      return u

The only relevant part of the implementation that needed to be changed was the way base URLs were created (makeServerURL()) while maintaining all of the other functionality. Besides the DefaultLinkGenerator you could also extend the CachingLinkGenerator, though depending on your use case the caching of links could come with a slew of new problems which is why we opted for the DefaultLinkGenerator.

With these two minor changes you keep the link generation in your upgraded Grails 2.x application backward compatible with how links were created in 1.3.7, and it also allows you to further modify parts of LinkGenerator to your liking.

Leave a Reply

Your email address will not be published. Required fields are marked *