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.
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.




Comments
Any updates?
Hi, I have just started architecting a web application from scratch and have decided to use struts2 as the base framework. Also, i have been planning to go the RESTful way for this application. However, I am not sure about how easy or difficult it would be to develop RESTfull applications using struts2.
I would appreciate if you could recommend some stable struts2 plug-ins that provide full support for RESTfull Application or tell about some relevant information on the internet regarding this.
rgds,
Sandee
Check out Struts 2.1
Sandee,
Thanks for your comment. I would check out the following page that describes the new REST plug-in for Struts 2.1: http://struts.apache.org/2.1.2/docs/rest-plugin.html. I don’t believe that Struts 2.1 had even gone to a beta release when I wrote this blog entry several months ago. I haven’t tried out this new plugin yet, but based on what I’ve read in its documentation as well as countless conversations on the Struts developers mailing list, RESTful support has been a priority for the 2.1 release. And more importantly, it was a priority for the developers to select a single implementation for REST support so that hopefully what you start developing on now will be supported for many releases to come. As for when 2.1 will be turned into a GA release, that’s hard to say. You’ll have to weigh the risks of that in the context of the release date for your project.
Hope that helps,
Steve
Anonymous comments
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.
how does RESTFUL approach help
I am new to RESTFUL approach.. I believe for security reasons it might be beneficial.apart from this is there any specific advantage that it might serve. Stateful http is one more feature it is heading towards I guess.If I am Planning to use it in my project what do you feel Would be the best approach.
Post new comment