days
-1
-3
hours
0
-5
minutes
-4
-8
seconds
-5
-2
search
Jooby gives you the right tools

Building large scale applications with Jooby

Edgar Espina

© Shutterstock / bogdanhoda

In this article, Edgar Espina, the creator of Jooby explains what options this modular micro framework offers to build large scale web applications and keep the source code organized and maintainable.

Overview

Jooby is a modular micro-framework for building web applications in Java and Kotlin. If this is the first time you heard about Jooby? Check out my previous article on why you should choose Jooby.

Micro-frameworks are ideal for building small applications, but are they good for medium or complex applications?

In this article, we’ll learn what options Jooby offers to build large scale web applications and keep the source code organized and maintainable.

SEE ALSO: What’s new in Jooby 1.1?

Initial application development

Let’s say you want to build a HTTP API for users. An initial application might look like:

import org.jooby.Jooby;

public class App extends Jooby {

  {
    /** User API: */
    use("/api/users")
      /** List all users. */
      .get(() -> {
        UserDao dao = require(UserDao.class);
        return dao.list();
      })
      /** Find user by ID. */
      .get("/:id", req -> {
        String id = req.param("id").value();
        UserDao dao = require(UserDao.class);
        return dao.get(id);
      })
      /** Create new user. */
      .post(req-> {
        User user = req.body(User.class);
        UserDao dao = require(UserDao.class);
        return dao.create(dao);
      })
      /** Delete by ID. */
      .delete(req-> {
        String id = req.param("id").value();
        UserDao dao = require(UserDao.class);
        return dao.delete(id);
      });
  }

  public static void main(String[] args) {
    run(App::new, args);
  }
}

Done! Your application is ready. It looks clean and easy to understand. Simplicity is one Jooby‘s goals.

Large scale application development

Suppose you want to extend your application with:

  • A HTTP API for blogs.
  • A HTTP API for comments.
  • UI for users, blogs and comments.

We can keep what we did before and extend the number of lines in our App.java. It is perfectly fine to do that, after all you own the application :)

But what if you want to better organize your source code? How can you? This is a missing feature for most micro-frameworks but not in Jooby.

Jooby provides some features and patterns that let you compose and organize your application.

MVC routes (a.k.a. Controller)

This is well-known pattern in Java frameworks were you annotated methods and make them accessible via HTTP:

import javax.inject.Inject;
import org.jooby.mvc.Path;
import org.jooby.mvc.GET;

@Path("/api/pets")
public class UserController {

  private UserDAO dao;

  @Inject
  public UserController(UserDAO dao) {
    this.dao = dao;
  }

  @Path("/:id")
  @GET
  public Pet get(String id) {
    return dao.get(id);
  }

  @GET
  public List<Pet> list() {
    return dao.list();
  }
}

We can replace the script routes with our new controller so now our application looks like:

import org.jooby.Jooby;

public class App extends Jooby {

  {
    use(UserController.class);
  }

  public static void main(String[] args) {
    run(App::new, args);
  }
}

You can apply the same pattern for blogs, comments and UI controllers:

import org.jooby.Jooby;

public class App extends Jooby {

  {
    /** UI: */
    use(WebPageController.class);
    /** User API: */
    use(UserController.class);
    /** Blog API: */
    use(BlogController.class);
    /** Comment API: */
    use(CommentController.class);
  }

  public static void main(String[] args) {
    run(App::new, args);
  }
}

This is how you usually split an application into multiple components using controller classes and annotations.
Wouldn’t be nice if we can keep the script routes and keep source code organized?

Embedded applications

This pattern let you compose one or more applications into one big application.

It’s perfect if you love script routes and want to keep them organized.

We split our application into 4 components, like we did before with controllers. But this time each component is an application by itself:

import org.jooby.Jooby;

public class Users extends Jooby {

  {
    /** User API: */
    use("/api/users")
      /** List all users. */
      .get(() -> {
        UserDao dao = require(UserDao.class);
        return dao.list();
      })
      /** Find user by ID. */
      .get("/:id", req -> {
        String id = req.param("id").value();
        UserDao dao = require(UserDao.class);
        return dao.get(id);
      })
      /** Create new user. */
      .post(req-> {
        User user = req.body(User.class);
        UserDao dao = require(UserDao.class);
        return dao.create(dao);
      })
      /** Delete by ID. */
      .delete(req-> {
        String id = req.param("id").value();
        UserDao dao = require(UserDao.class);
        return dao.delete(id);
      });
  }
}

Time to embedded it as we as we did in the controller example:

import org.jooby.Jooby;

public class App extends Jooby {
  {
    /** UI: */
    use(new WebPages());
    /** User API: */
    use(new Users());
    /** Blog API: */
    use(new Blogs());
    /** Comment API: */
    use(new Comments());
  }

  public static void main(String[] args) {
    run(App::new, args);
  }
}

This approach let you build standalone applications that can be easily embedded into a major application.

Conclusion

In this article, we illustrated how to build large scale applications with Jooby using MVC or script routes.

Some users prefer MVC routes over script routes, but the truth is that you can use both or mix them togetherJooby just makes sure to give you the right tools. Like always, the decision is yours.

That’s all for now. Thank you for reading! If you are not yet convinced about Jooby here is a nice article on why you should give it a try:

 

This post was originally published on Hacker Noon

Author

Edgar Espina

Edgar is the creator of Jooby. He is also an Open Source enthusiast.

Find him on Twitter: @edgarespina


Leave a Reply

Be the First to Comment!

avatar
400
  Subscribe  
Notify of