Javalin¶
We will be using a Java library called Javalin for building our REST API.
You need to add the following to the dependencies
clause of build.gradle
:
1 2 | compile 'org.slf4j:slf4j-simple:1.8.0-beta4'; compile 'io.javalin:javalin:3.7.0'; |
- Javalin needs
slf4j
(for logging).
Run a server locally!¶
Running Javalin is as easy as follows!
1 2 3 4 5 6 | public class ApiServer { public static void main(String[] args) { Javalin app = Javalin.create().start(7000); app.get("/", ctx -> ctx.result("Hello World!")); } } |
After running the ApiServer
, point your browser to http://localhost:7000/
to see the following message!
1 | Hello World! |
Notice the following statement
1 | app.get("/", ctx -> ctx.result("Hello World!")); |
The ->
is part of Java's Lambda syntax. This syntax may be strange to you! If that is the case, read the next section (otherwise skip it).