ExpressoJ

A Minimal Java Web Framework

SHOW ME THE CODE

Here's a sample application that uses the .use() method to intercept all requests and handle GET request on /hello endpoint.

public static void main(String[] args) {
    /* Initialize Expresso instance */
    Expresso expresso = Expresso.init();

    /*
        * The .use() executes middleware method for all incoming requests, regardless
        * of the HTTP method.
        * A specific path can also be specified if needed.
        */
    expresso.use((req, res, ctx) -> {
        log.info("Request Intercepted at : {}", req.getRequestPath());
        ctx.executeNextMiddleware();
    });

    /* Support for [get, post, put, delete] http methods */
    expresso.get("/hello", (req, res, ctx) -> {
        res.writeResponse("Hello world");
    });

    /* Set a port to listen on */
    expresso.listenOnPort(5757);

    /* Start the application */
    expresso.startServer();
}

GET STARTED

Follow the steps below to use Expresso in your project.

1. Clone ExpressoJ GitHub repository to your system.

GitHub: ExpressoJ repo

$ git clone https://github.com/neerajsurjaye/ExpressoJ.git

2. Navigate into the project and switch to main branch.

$ cd ~/ExpressoJ
$ git checkout main

3. Build the project.

# Builds the project and adds it into your local maven repository
$ mvn clean install

4. Now add ExpressoJ as a dependency in your own project.

<dependencies>
    ...
    <dependency>
        <groupId>com.spec.web</groupId>
        <artifactId>expressoj</artifactId>
        <version>1.0.0</version>
    </dependency>
    ...
</dependencies>

5. (Optional)If building a standalone UBER/FAT jar. In your project update the following properties in pom.xml.

  1. Ensure build type is jar.
  2. Use maven-shade-plugin to make it a uber(fat) jar.
  3. Use maven-jar-plugin to add a manifest to main class.
<!-- Jar packaging required -->
<packaging>jar</packaging>

<build>
    ...
    <plugins>
        <!-- Maven shade plugin: Required to create uber(fat) 
        JAR that includes all the dependencies -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <!-- Maven jar plugin: Adds a manifest file specifying 
        the main class. -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <archive>
                    <manifest>
                        <!-- Change this to fully qualified name of 
                        your main class -->
                        <mainClass>com.spec.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
    ...
</build>

6. Write, build and run the project using Maven

# Build using maven
$ mvn clean install

# Execute the program
$ java -jar target/expresso-program.jar