Versions Compared

Key

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

...

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

struts.rest.handlerOverride.EXTENSION

The alias for the ContentTypeHandler implementation that handles the EXTENSION value

N/A

Any declared alias for a ContentTypeHandler implementation

struts.rest.defaultExtension

The default extension to use when none is explicitly specified in the request

xml

Any extension

struts.rest.validationFailureStatusCode

The HTTP status code to return on validation failure

400

Any HTTP status code as an integer

struts.rest.namespace

Optional parameter to specify namespace for REST services

/

eg. /rest

struts.rest.content.restrictToGET

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

true

eg. put struts.rest.content.restrictToGET = false in struts.properties

Installation

...

Resources

...