...
Table of Contents | ||||
---|---|---|---|---|
|
Overview
The REST Plugin
Excerpt |
---|
provides high level support for the implementation of RESTful resource based web applications |
If you prefer to see a working code example, instead of reading through an explanation, you can download the struts2 sample apps and check out the struts2-rest-showcase
application, a complete WAR file, that demonstrates a simple REST web program.
...
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.
...
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.
...
Code Block |
---|
struts.rest.handlerOverride.xml=myXml |
Use Jackson framework as JSON ContentTypeHandler
The default JSON Content Handler is build on top of the JSON-lib. If you prefer to use the Jackson framework for JSON serialisation, you can configure the JacksonLibHandler as Content Handler for your json requests.
First you need to add the jackson dependency to your web application by downloading the jar file and put it under WEB-INF/lib or by adding following xml snippet to your dependencies section in the pom.xml when you are using maven as build system.
Code Block | ||
---|---|---|
| ||
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
|
Now you can overwrite the Content Handler with the Jackson Content Handler in the struts.xml:
Code Block | ||
---|---|---|
| ||
<bean type="org.apache.struts2.rest.handler.ContentTypeHandler" name="jackson" class="org.apache.struts2.rest.handler.JacksonLibHandler"/>
<constant name="struts.rest.handlerOverride.json" value="jackson"/>
<!-- Set to false if the json content can be returned for any kind of http method -->
<constant name="struts.rest.content.restrictToGET" value="false"/>
<!-- Set encoding to UTF-8, default is ISO-8859-1 -->
<constant name="struts.i18n.encoding" value="UTF-8"/>
|
Settings
The following settings can be customized. See the developer guide.
For more configuration options see the Convention Plugin Documentation
...