A micro-web framework that provides a simple and elegant way of creating web applications programatically. Jogger brings the best ideas of other frameworks (Express.js, Ruby on Rails, Sinatra and Flask) to create a solution that doesn't feel foreign in the Java language.
Jogger can be integrated with the Spring framework. It can also run on Heroku.
To get started, first include the Jogger library and then create a class with a main method like this:
publicclassMain{publicstaticvoidmain(String[] args) throwsException{RouterMiddlewarerouter = newRouterMiddleware(); router.get("/", newRouteHandler(){@Overridepublicvoidhandle(Requestrequest, Responseresponse){response.write("<h1>Hello World!</h1>")} }); Joggerapp = newJogger(router); app.listen(5000); app.join()} }Run it and point your browser to http://localhost:5000. There you go, you've just created your first Jogger app!
There is also a Maven archetype that will help you get started in seconds.
In the previous example, we instantiated a RouterMiddleware and then passed it to the Jogger constructor. Middlewares are implementations of the org.jogger.Middleware interface that provide some functionality to the request/response lifecycle. Currently, there are two built-in middlewares:
- StaticMiddleware: serves static files (CSS, Javascript, images, etc.). Learn more.
- RouterMiddleware: routes requests to Java methods (controllers and actions). Learn more.
The following example shows how middlewares are instantiated and passed to the Jogger instance:
publicclassMain{publicstaticvoidmain(String[] args) throwsException{// assuming you have all your static files in a folder called publicStaticMiddlewarestatik = newStaticMiddleware("public"); RouterMiddlewarerouter = newRouterMiddleware(); // add routes// instantiate Jogger with both middlewaresJoggerapp = newJogger(statik, router); app.listen(5000); app.join()} }If you run Jogger without providing at least one middleware, every request will return 404 Not Found response.
You can also create your own middlewares by implementing the org.jogger.Middleware interface. However, most of the times you'll be using interceptors instead (which are part of the RouterMiddleware).
By default, Jogger is configured with a view template engine called Freemarker but Jade is also supported.
You can render templates from your controllers by calling the render(String templateName) method of the org.jogger.http.Response class. Let's take a look at the following example:
publicclassPages{publicvoidindex(Requestrequest, Responseresponse){response.render("index.ftl")} }This will render the index.ftl file in the root of your project. You can learn more about how to render templates here.
Jogger provides a testing framework that allows you to create integration tests easily. Let's see how this works with an example:
publicclassPagesTestextendsJoggerTest{@TestpublicvoidshouldRenderIndex() throwsException{MockResponseresponse = get("/").run(); Assert.assertEquals( response.getStatus(), Response.OK ); Assert.assertEquals( response.getRenderedTemplate(), "index.ftl" ); Assert.assertTrue( response.getOutputAsString().contains("test") )} @OverrideprotectedJoggergetJogger(){Joggerapp = newJogger(); // add configuration, routes and interceptorsreturnapp; // a better approach is to have a JoggerFactory class that creates our Jogger object. } }You can learn more about testing here.
You can find the API docs here.