Write Spring-less Java web apps as simple as on Node.js
When thinking of Java web applications, what comes to mind first? Spring, endless annotations, XML configurations,
containers, layers upon layers of abstractions, and boilerplate code. A lot of boilerplate code. Node.js approach seems
to be so much simpler, and this is probably one of the reasons newbies prefer Node.js to Java. But what if I told you
Java can be that simple too?
To not sound unfounded and make everything illustrative, let’s rewrite a personal blog from this article and make it
slightly different. Instead of Node.js for backend, we will write it in Java using DataKernel framework. The other parts
of the implementation will stay the same. So, you can easily compare both approaches for the same task.
The final result will still look like this:
Note, the frontend part won’t be described here as we just leave it the same as in the original source,
thanks to the author. You can add it later on by yourself.
Let’s nail out the basics. You should probably have some experience with Java to successfully complete the tutorial.
Also make sure that you have all the necessary prerequisites installed:
At this point we’re ready to dive into backend implementation.
Create the new Maven project in the IDE. Your project structure should look like this:
Then add Maven dependency to your project by configuring your pom.xml file in the following way:
Make sure that your project SDK is set 1.8+
We are going to set up our server in the src/main/java folder. Let’s create a BlogLauncher.java class here. It will
incorporate the main logic of our app and take care of application lifecycle.
Instead of middleware chains, DataKernel provides another concept named servlets. You don’t need an Express Router
anymore, we will use RoutingServlet to handle all the endpoints.
To handle requests independently and preserve modularity we create three async servlets:
staticServlet - takes care of our front
failServlet - handles possible errors
rootServlet - uses both of them, returns a RoutingServlet with CRUD operations processing.
So your BlogLauncher should look like this:
Looking at the above code, you can note:
DataKernel provides powerful Dependency Injection module - we just mark the needed provider methods with the @Provides
annotation and all the required components will be created automatically.
Usually these recipes can be put in the separate Module and then taken right in the place where we need them. Like
DbModule in the getBusinessLogicModule(). Wait a second and we will deal with the DbModule and other configs.
DataKernel supports the Promise concept. For example, in GET processing, Promise.of() will return a successfully created
Promise with the given content.
Since BlogLauncher extends HttpServerLauncher, we can easily launch our server with the help of the launch
method in the main method.
To make this example look similar to the application from the Node.js tutorial we will use MongoDB too, but with the Java
Let’s add some new classes, so the structure of src/main/java directory will eventually look like this:
Add a blog resource bundle in the resources directory with the following content:
In the DbModule.java we will provide MongoDB configurations:
Note, schemaDaoDb() is marked with @Export annotation which allows us to explicitly say which instances we want to be
exported from the module. ArticleSchema, ArticleSchemaDao and ArticleSchemaDaoDb that are used here will be shown next.
In ArticleSchemaDao.java we will simply create an interface which will be implemented in the ArticleSchemaDaoDb,
and also the ArticleSchema - a plain java class with fields, constructors, and accessors.
Another important point is that DataKernel provides a Codec module which encodes and decodes custom objects. That’s
quite important in our example since we work with JSON and want to encode/decode ArticleSchema objects to/from JSONs.
For this purpose, let’s write down the following code in the ArticleCodecs:
All we need is to add the frontend part and that’s it! Create a new client directory alongside src directory and
fill it with this content. Don’t forget to install
dependencies with npm i in the client directory. It should look like this:
Running the application
Now let’s test the application.
Don’t forget to start MongoDB:
Run npm run build from the client directory.
Move generated dist folder to the resources in src/main.
Run BlogLauncher.main(), then open your favourite browser and go to localhost:8080.
Congratulations, you’ve just created a Java backend for the personal blog app. It wasn’t that hard, right?
What is next?
To see how you can use DataKernel, take a look at DataKernel
tutorials or docs.
Thank you for reading this tutorial and if you have faced any problems, take into account the full example sources on GitHub.