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();
}
GitHub: ExpressoJ repo
$ git clone https://github.com/neerajsurjaye/ExpressoJ.git
$ cd ~/ExpressoJ
$ git checkout main
# Builds the project and adds it into your local maven repository
$ mvn clean install
<dependencies>
...
<dependency>
<groupId>com.spec.web</groupId>
<artifactId>expressoj</artifactId>
<version>1.0.0</version>
</dependency>
...
</dependencies>
<!-- 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>
# Build using maven
$ mvn clean install
# Execute the program
$ java -jar target/expresso-program.jar