Versions Compared

Key

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

Overview

The Rest REST Plugin

Excerpt

simplifies RestREST-style resources to be consumed by both human and machine clients

. The goals are to 100% follow Ruby on Rails-style Rest REST URL conventions and enable XML-free resource development. It builds on the Codebehind Plugin to support automatic configuration of both actions and results.

...

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

...

...