Skip to content

Quick start Struts

sapessi edited this page Dec 26, 2018 · 13 revisions

You can use the aws-serverless-java-container library to run a Apache Struts2 based application in AWS Lambda. You can use the library within your Lambda handler to load your Struts application and proxy events to it.

Maven archetype

You can quickly create a new serverless Struts2 application using our Maven archetype. First, make sure Maven is installed in your environment and available in your PATH. Next, using a terminal or your favorite IDE create a new application, the archetype groupId is com.amazonaws.serverless.archetypes and the artifactId is aws-serverless-struts2-archetype;

mvn archetype:generate -DgroupId=my.service -DartifactId=my-service -Dversion=1.0-SNAPSHOT \
       -DarchetypeGroupId=com.amazonaws.serverless.archetypes \
       -DarchetypeArtifactId=aws-serverless-struts2-archetype \
       -DarchetypeVersion=1.3

The archetype sets up a new maven project. The pom.xml includes the dependencies you will need to build a basic Struts API that can consume and product JSON data. The generated code includes an actions package with a /ping resource; and a set of unit tests that exercise the application.

The project also includes a file called sam.yaml. This is a SAM template that you can use to quickly test your application in local or deploy it to AWS. Open the README.md file in the project root folder for instructions on how to use SAM Local to run your Serverless API or deploy it to AWS.

Manual setup / Converting existing projects

1. Import dependencies

The first step is to import the Struts2 implementation of the library:

<dependency>
    <groupId>com.amazonaws.serverless</groupId>
    <artifactId>aws-serverless-java-container-struts2</artifactId>
    <version>1.3</version>
</dependency>

This will automatically also import the aws-serverless-java-container-core and aws-lambda-java-core libraries.

Publish your Lambda function

You can follow the instructions in AWS Lambda's documentation on how to package your function for deployment.

Using Struts2 plugins can end into problems when you bundle your projects with the Maven Shade plugin to an Uber JAR because of conflicting struts-plugin.xml file in each plugin jar file. The recommended way for Apache Struts would be creating a **.zip** deployment file with the **maven-assembly-plugin**.

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.0</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assembly/dist.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>lambda</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>lambda</id>
    <formats>
        <format>zip</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <dependencySets>
        <dependencySet>
            <outputDirectory>lib</outputDirectory>
            <useProjectArtifact>false</useProjectArtifact>
        </dependencySet>
    </dependencySets>
    <fileSets>
        <fileSet>
            <directory>${basedir}/src/main/resources</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>*</include>
            </includes>
        </fileSet>
        <fileSet>
            <directory>${project.build.directory}/classes</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>**/*.class</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

Deploy your Lambda function

  1. Run mvn install to build your deployment ZIP file
  2. In AWS Console or over AWS CLI deploy generated ZIP file
  3. Use com.amazonaws.serverless.proxy.struts2.Struts2LambdaHandler::handleRequest as Handler
Clone this wiki locally