Overview
The Rest REST Plugin
Excerpt |
---|
simplifies RestREST-style resources to be consumed by both human and machine clients |
...
HTTP method | URI | Class.method | parameters |
---|---|---|---|
GET | /movie | Movie.index | |
POST | /movie | Movie.create | |
PUT | /movie/Thrillers | Movie.update | id="Thrillers" |
DELETE | /movie/Thrillers | Movie.destroy | id="Thrillers" |
GET | /movie/Thrillers | Movie.show | id="Thrillers" |
GET | /movie/Thrillers/edit | Movie.edit | id="Thrillers" |
GET | /movie/new | Movie.editNew |
In addition to being a RestREST-style URL mapper, this plugin provides built-in support for multiple content types, switchable through the URL extension. In this way, a single resource can be exposed as multiple content types without any extra work.
...
Code Block | ||
---|---|---|
| ||
package org.apache.struts2.rest.example; public class OrdersResourceOrdersController implements ModelDriven<Order> { private OrderManager orderManager; private String id; private Order model; // Handles /orders/{id} GET requests public RestInfoHttpHeaders show() { model = orderManager.findOrder(id); return new DefaultRestInfo() .renderResult(DefaultHttpHeaders("show") .withETag(model.getUniqueStamp()) .lastModified(model.getLastModified()); } // Handles /orders/{id} PUT requests public String update() { orderManager.updateOrder(model); return "update"; } // getters and setters } |
In this example, the ModelDriven
interface is used to ensure that only my model, the Order object in this case, is returned to the client, otherwise, the whole OrdersResource
OrdersController
object would be serialized.
You may wonder why the show()
method returns a RestInfo
HttpHeades
object and the update()
method returns the expected result code String. Well, the Rest Plugin adds in support for action methods that return RestInfo
objects as a way for the action to have more control over the response. In this example, we wanted to ensure the response included the ETag header and a last modified date so that the information will be cached properly by the client. The RestInfo
object is a convenient way to control the response in a type-safe way.
...
Code Block | ||
---|---|---|
| ||
<bean name="myXml" type="org.apache.struts2.rest.handler.ContentTypeHandler" class="com.mycompany.MyXmlContentHandler" /> |
Then, tell the Rest REST Plugin to override the handler for the desired extension with yours. In struts.properties
, it would look like this:
...
The plugin ships with a struts2-rest-showcase
application that demonstrates a simple Rest REST web program.
Settings
The following settings can be customized. See the developer guide.
...
- http://www.b-simple.de/documents - Short RESTful rails PDF in multiple languages
- Restful RESTful Web Services - Highly recommend book from O'Reilly
...