Customizing URL Formats in Grails (Making your URLs case insensitive)

When I started working on Grails I quickly noticed one caveat in its URL patterns. I wonder why on the Earth anybody would want such camel casing in URLs. Misreading the case sensitive nature of a Grails app URL can lead to broken links and 404 errors. That means you need to be careful when playing with how your URLs look like.

For example:

http://localhost:8080/myapp/someLong/someAction

works perfectly fine.

But when I entered following URL patterns

http://localhost:8080/myapp/somelong/someaction
or

http://localhost:8080/myapp/SomeLong/SOMEACTION

I get a HTTP Status 404 that is “Page Not found” message.

It does not matter what part I write in lower case it will just throw same HTTP Status code.

The culprit here is routing engine of Grails. It’s configured to use the default settings. And how does Grails do that? Simply by implementing the grails.web.UrlConverter interface in a class named grails.web.CamelCaseUrlConverter. But as always this is just a convention not a requirement. So I went ahead and changed this by plugging in a new behavior or converter.

So I created a new converter named LowerCaseUrlConverter. The name need not contain UrlConverter suffix, it’s here just to tell us that it implements some
UrlConverter. So here is the code:

[sourcecode language=”java”]
package com.myapp.utils

import grails.web.UrlConverter

class LowerCaseUrlConverter implements UrlConverter {

@Override
String toUrlElement(String propertyOrClassName) {
if (propertyOrClassName) {
// If there is some class name or method name in url then convert it to lowercase.
propertyOrClassName.toLowerCase()
} else {
// Else return default name.
propertyOrClassName
}
}
}
[/sourcecode]

Now put this class in src/groovy folder because it’s a Groovy file named LowerCaseUrlConverter.groovy.

Next is to configure this bean so that Grails knows it needs to use new implementation of grails.web.UrlConverter, so that other parts of Grails can generate
lowercase URLs from its g.createLink() and redirect() and other methods.

So open up grails-app/conf/spring/resources.groovy file, and put inside beans closure this line:

[sourcecode language=”java”]

"${grails.web.UrlConverter.BEAN_NAME}" (com.myapp.utils.LowerCaseUrlConverter)

[/sourcecode]

And that’s all we need to do. Now we are good to go with all lowercase URLs

http://localhost:8080/myapp/somelong/someaction

You can even check it with some more combinations like:

http://localhost:8080/myapp/Somelong/Someaction
http://localhost:8080/myapp/someLong/someAction

or if you are crazy enough and gone nuts you can also try this:

http://localhost:8080/myapp/SoMeLoNg/sOmEaCtIoN

One last thing.
I didn’t tell you about one more class that Grails has up its sleeves. And that’s the grails.web.HyphenatedUrlConverter class. You can guess by name what it does.

One word of caution: You can’t have more than one implementation of URLConverter in grails-app/conf/spring/resources.groovy file. However default one will always work regardless if you provide your implementation or not!

Now I feel safer with my URLs… 😉

Manvendra Singh

My LinkedIn

@Manvendra_SK

FOUND THIS USEFUL? SHARE IT

Leave a Reply

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