Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  • Ruby on Rails REST-style URLs
  • Zero XML config when used with Convention Plugin
  • Built-in serialization and deserialization support for XML and JSON
  • Automatic error handling
  • Type-safe configuration of the HTTP response
  • Automatic conditional GET support

RestActionMapper

The main functionality of the REST plugin lies in the interpretation of incoming request URL's according the RESTful rules. In the Struts 2 framework, this 'mapping' of request URL's to Actions is handled by in implementation of the ActionMapper interface. Out of the box, Struts 2 uses the DefaultActionMapper to map URL's to Actions via the logic you are probably already familiar with.

Note

Actions or Controllers? When writing Struts 2 apps, most of a developer's time in consumed with createing Action classes. Actions are the targets of URL's and encapsulate the invocation of the appropriate business logic for a given request. In the context of the REST plugin, we'll adopt the RESTful lingo and refer to our Actions as Controllers. Don't be confused; it's just a name!

The REST plugin provides an alternative implementation, RestActionMapper, that provides the RESTful logic that maps a URL to a give action class ( aka 'controller' in RESTful terms ) and, more specifically, to the invocation of a method on that controller class. The following section, which comes from the Javadoc for the class, details this logic.

...

The REST plugin automatically handles serialization to, and deserialization from, each format.

Usage

Create Java objects ending in "Controller" in the configured package. The "Controller" suffix is used to distinguish REST action resources from regular Struts 2 actions, although it is completely optional and they are functionally the same. Now, add in methods to handle the various requestsyour "Controllers" with the target methods indicated in the RESTful URL to action/method logic described above. For example, the following resource action will support /orders/34 GET and PUT requests:

...