Versions Compared

Key

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

...

Note, you can download the jars for these plugins from Maven Central

Configuration ( struts.xml )

Just dropping the plugin's into your application may not produce exactly the desired effect. There are a couple of considerations. The first consideration is whether you want to have any non-RESTful URL's coexisting with your RESTful URL's. We'll show two configurations. The first assumes all you want to do is REST. The second assumes you want to keep other non-RESTful URL's alive in the same Struts 2 application.

As with all configuration of Struts 2, we prefer using <constant/> elements in our struts.xml.

REST Only

Instruct Struts to use the REST action mapper; note, at this point, the REST mapper is the only mapper, so other URL styles won't work:

Code Block
xml
xml

<constant name="struts.mapper.class" value="rest" />

We're relying on the Convention plugin to find our controllers, so we need to configure the convention plugin a bit:

Code Block
xml
xml

<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>
<constant name="struts.convention.package.locators" value="example"/>
Note

Note, you don't have to use the Convention plugin just to use the REST plugin. The actions of your RESTful application can be defined in XML just as easy as through conventions. The REST mapper doesn't care how the application came to know about your actions when it maps a URL to an invocation of one of it's methods.

REST and non-RESTful URL's Together

If you want to keep using some non-RESTful URL's alongside your REST stuff, then you'll have to provide for a configuration that utilizes to mappers.

Create Controller Actions

...

Code Block
struts.rest.handlerOverride.xml=myXml

struts.xml

Instruct Struts to use the REST action mapper:

...


<constant name="struts.mapper.class" value="rest" />

Because the REST plugin uses the Convention plugin, some settings need to be set in struts.xml:

...


<constant name="struts.convention.action.suffix" value="Controller"/>
<constant name="struts.convention.action.mapAllMatches" value="true"/>
<constant name="struts.convention.default.parent.package" value="rest-default"/>

and for this example:

Code Block
xmlxml

<constant name="struts.convention.package.locators" value="example"/>

Example

The plugin ships with a struts2-rest-showcase application that demonstrates a simple REST web program.

...