RESTful URL Support in Apache Struts 2

With the explosion of web frameworks such as Ruby on Rails that advocate (and even dictate) that you use RESTful URLs for your applications, I was shocked when I discovered that Apache’s Struts 2 Java web framework does not provide near the same level of support for this behavior. But before this talk about REST puts you to sleep, I better explain what the heck I’m talking about! REST stands for REpresentational State Transfer, and you can read all the details here.

Steven Merrill, Director of Devops
#API | Posted

With the explosion of web frameworks such as Ruby on Rails that advocate (and even dictate) that you use RESTful URLs for your applications, I was shocked when I discovered that Apache’s Struts 2 Java web framework does not provide near the same level of support for this behavior.

But before this talk about REST puts you to sleep, I better explain what the heck I’m talking about!

REST stands for REpresentational State Transfer, and you can read all the details here.

Basically, it’s an architecture for representing resources via a global identifier, and recognizes that there are a set of actions to perform on these resources.

So when translated to web applications, this means that your URLs will contain all of the details of the request in the URI itself, and will not rely upon query string parameters to identify the resource.

Here’s an example of two different URLs:

  • Typical Struts 2 URL: http:////user/edit.action?id=7
  • RESTful URL: http:////user/7/edit

The first couple of attempts at providing support for REST in Struts 2 were accomplished via the creation of specialized ActionMapper classes. These

are responsible for interpreting the URLs of the application and invoking the correct Action classes: RestfulActionMapper and Restful2ActionMapper.

While these action mappers met some of the requirements for supporting RESTful URLs, they had some shortcomings.

Most importantly, they failed to encourage “convention over configuration”, which means that if a certain naming convention is followed, additional configuration of the actions and results for each action is not required.

The next attempt at supporting REST in Struts 2 is included in the 2.1.1 release, which has not been marked as Generally Available yet.

In this release, a Struts plugin has been created that provides full support of handling RESTful URLs.

It also utilizes the Codebehind plugin to eliminate the need for configuration via XML files or annotations.

Another excellent feature of this plugin is its support for automatic serialization to XML or JavaScript Object Notation (JSON).

While my initial tests with this plugin were successful, the one glaring exclusion at this point appears to be support for easily generating RESTful URLs via the Struts url tag.

Also, there is some uncertainty as to how some of our framework code and other functionality like SiteMesh and validation will fit into the overall picture when using the plugin.

RESTful URLs are here to stay, and the new Struts plugin appears to do an excellent job of bringing Struts 2 into the mix of frameworks supporting them.

Once the last few bells and whistles get added in and we have a chance to kick the tires a little more, I’m confident we will be adopting it in the near future.

If you have had any success (or failure) in using RESTful URLs in your Struts 2 application, please leave a comment explaining your experiences and thoughts.

Steven Merrill

Director of Devops