Overview
The Rest Plugin
Excerpt |
---|
simplifies Rest-style resources to be consumed by both human and machine clients |
...
and the framework handles the serialization and deserialization automatically.
Features
- Fully implements Ruby-On-Rails Rest-style URLs
- Supports XML-free development, without requiring annotations
- Built-in serialization support for XML and JSON
- Automatic handling of errors
- Type-safe configuration of the HTTP response
- Automatic conditional GET support
Usage
As with the Codebehind Plugin, the first step is to tell the plugin where to find your resource action classes. Modify the configuration of your Struts 2 filter in web.xml
and add the 'actionPackages' init-param like so:
...
Also, notice we aren't returning the usual "success" result code in either method. This allows us to use the special features of the Codebehind Plugin to intuitively select the result template to process when this resource is accessed with the .xhtml
extension. In this case, we can provide a customized XHTML view of the resource by creating /orders-show.jsp
and /orders-update.jsp
for the respective methods.
Custom ContentTypeHandlers
If you need to handle extensions that aren't supported by the default handlers, you can create your own ContentTypeHandler
implementation and define it in your struts.xml
:
...
Code Block |
---|
struts.rest.handlerOverride.xml=myXml |
Example
The plugin ships with a struts2-rest-showcase
application that demonstrates a simple Rest web program.
Settings
The following settings can be customized. See the developer guide.
Setting | Description | Default | Possible Values |
---|---|---|---|
| The alias for the | N/A | Any declared alias for a |
| The default extension to use when non is explicitly specified in the request |
| Any extension |
Installation
This plugin can be installed by copying the plugin jar into your application's /WEB-INF/lib
directory. It does depend on the Codebehind Plugin, so if aren't using a build system that supports transitive dependencies like Maven 2, you will need to add that plugin as well.
Resources
- http://www.b-simple.de/documents - Short RESTful rails PDF in multiple languages
- Restful Web Services - Highly recommend book from O'Reilly
Version History
From Struts 2.1.1+