...
Create Controller Actions
Once everything is configured, you need to create the controllers. Controllers are simply actions created with the purpose of handling requests for a give RESTful resource. As we saw in the mapping logic above, various REST URL's will hit different methods on the controller. Traditionally, normal Struts 2 actions expose the execute
method as their target method. Controllers on the other hand . . Here's a sample controller for a orders resource. Note, it doesn't implement all of the possible REST URL to method mappings.
Code Block | ||
---|---|---|
| ||
package org.apache.struts2.rest.example; public class OrdersController implements ModelDriven<Order> { private OrderManager orderManager; private String id; private Order model; // Handles /orders/{id} GET requests public HttpHeaders show() { model = orderManager.findOrder(id); return new DefaultHttpHeaders("show") .withETag(model.getUniqueStamp()) .lastModified(model.getLastModified()); } // Handles /orders/{id} PUT requests public String update() { orderManager.updateOrder(model); return "update"; } // getters and setters } |
...
Setting | Description | Default | Possible Values |
---|---|---|---|
| The alias for the | N/A | Any declared alias for a |
| The default extension to use when none is explicitly specified in the request | | Any extension |
| The HTTP status code to return on validation failure | | Any HTTP status code as an integer |
| Optional parameter to specify namespace for REST services | | eg. /rest |
| Optional parameter, if set to true blocks returning content from any other methods than GET, if set to false, the content can be returned for any kind of method | | eg. put struts.rest.content.restrictToGET = false in struts.properties |
Installation
...
Resources
- http://www.b-simple.de/documents - Short RESTful Rails tutorial (PDF, multiple languages)
- RESTful Web Services - Highly recommend book from O'Reilly
- Go Light with Apache Struts 2 and REST - Presentation by Don Brown at ApacheCon US 2008
...