JHipster – from fast prototyping to production ready services

Sebastian Fabisz

JHipster is an open source project created to speed up the development and deployment of web applications. Supported by almost 600 contributors – it’s a great tool to produce any kind of Proof of Concept, CRUD as well as production ready services in a modern and rapid way. If this kind of functionality isn’t what you are looking for – it still can be used to learn some of the best development patterns by analyzing the generated source code.

Theory…

I will start from explaining what JHipster can do. This is basically a project source code generator with options to select architecture pattern and technology stack based on Java and SpringBoot. Sounds pretty simple, right, but I think any developer that wants to start a new project from the very beginning will have to face and harness a great number of boilerplate code. All of the configuration classes, project management, testing, deployment to name a few. At the point when deadline is short, doing this kind of things seems like waste of time. Here comes JHipster generator. Run JHipster from command line, name your project, then select the backend and frontend technology stack, database, testing framework and wait a few minutes until the project source code is generated and ready to deploy on many popular cloud services.

… and practice

After some short introduction let’s dive straight into the JHipster workflow. We will start by creating a simple monolith web application called Task Board. Here I will try to explain more about the possibilities this tool provides. Before we can start, our operating system should already have installed Java 11 and Node.js in the latest LTS version.

Run below command to install the latest stable version of the JHipster generator using npm:

npm install -g generator-jhipster

or yarn:

yarn global add generator-jhipster

(It is also possible to use an online tool or Docker image)

Whichever of the above options you choose it will allow you to use JHipster globally in the system terminal. After successful installation the next step is to create a directory for our project, otherwise the project will be created in home directory. This is important as many people think a generator creates a new folder by default.

mkdir task-board
cd task-board

Inside the project directory we are ready to run jhipster command, and then the welcome screen should be presented. Here we can start to tailor configuration to our needs.

(base) ➜ task-board jhipster
INFO! Using JHipster version installed globally
INFO! Executing jhipster:app


        ██╗ ██╗   ██╗ ████████╗ ███████╗   ██████╗ ████████╗ ████████╗ ███████╗
        ██║ ██║   ██║ ╚══██╔══╝ ██╔═══██╗ ██╔════╝ ╚══██╔══╝ ██╔═════╝ ██╔═══██╗
        ██║ ████████║    ██║    ███████╔╝ ╚█████╗     ██║    ██████╗   ███████╔╝
  ██╗   ██║ ██╔═══██║    ██║    ██╔════╝   ╚═══██╗    ██║    ██╔═══╝   ██╔══██║
  ╚██████╔╝ ██║   ██║ ████████╗ ██║       ██████╔╝    ██║    ████████╗ ██║  ╚██╗
   ╚═════╝  ╚═╝   ╚═╝ ╚═══════╝ ╚═╝       ╚═════╝     ╚═╝    ╚═══════╝ ╚═╝   ╚═╝

                            https://www.jhipster.tech

Welcome to JHipster v6.10.5
Application files will be generated in folder: /Users/work/Desktop/workspace/jhipster/task-board
 _______________________________________________________________________________________________________________

  Documentation for creating an application is at https://www.jhipster.tech/creating-an-app/
  If you find JHipster useful, consider sponsoring the project at https://opencollective.com/generator-jhipster
 _______________________________________________________________________________________________________________

? Which *type* of application would you like to create? (Use arrow keys)
❯ Monolithic application (recommended for simple projects) 
  Microservice application 
  Microservice gateway 
  JHipster UAA server 

       

The first and probably most important step is to decide what type of application is desired. Basically there are two options – monolith and micro-service architecture. Monolith is simple. It’s all-in-one package. For micro-services JHipster provides options to create regular service, gateway and UAA server. The gateway will be responsible for exposing and securing application traffic. UAA server is responsible for the user’s authorization and authentication. Here we can already see it’s not just a toy, but a serious tool that takes care of security and good practices and can be used in production environment. Lets’s choose the Monolith application as our project is a simple task board.

? [Beta] Do you want to make it reactive with Spring WebFlux? (y/N) N

This might be a good option for someone who needs to test a reactive solution for another project. For us it’s not required.

The next two questions require us to provide the application name and root package for the source code. After that – the question about JHipster Registry will appear.

?What is the base name of your application? TaskBoard
? What is your default Java package name? com.jhipster.taskboard
? Do you want to use the JHipster Registry to configure, monitor and scale your application? (Use arrow keys)
❯ No
Yes

JHipster Registry is a runtime application which combines three responsibilities. First – it acts as Eureka Server to enable service discovery and scaling. Second – it implements Spring Cloud Config server to provide runtime configuration for all services. And third – it’s an administration application with dashboard. For us service scaling is not required so we can give up this one.

In the following step it’s time to decide which type of authentication type fits best.

? Which *type* of authentication would you like to use? (Use arrow keys)
❯ JWT authentication (stateless, with a token)
HTTP Session Authentication (stateful, default Spring Security mechanism)
OAuth 2.0 / OIDC Authentication (stateful, works with Keycloak and Okta)

We can choose from one stateless and two stateful solutions. Descriptions will tell us which technology is used underneath to implement each option. JWT is light and fast, as our task board should be, so we can choose this one.

? Which *type* of database would you like to use? (Use arrow keys)
❯ SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL)
MongoDB
Cassandra
Couchbase
[BETA] Neo4j
No database
? Which *production* database would you like to use? MySQL
? Which *development* database would you like to use?
H2 with disk-based persistence
❯ H2 with in-memory persistence
MySQL

Next stop and it’s time to choose the database engine. The list contains relational, document and graph databases. For any external dependency JHipster will take care of secure configuration. If any database is selected then our final project will include script migration tool, DTO for transport layer and docker-compose file with properly configured database base image. This allows to start development of an application without taking care of external dependencies. I selected MySQL for production and in-memory H2 for development purposes of our application.

? Do you want to use the Spring cache abstraction?
[BETA] Yes, with the Infinispan implementation (hybrid cache, for multiple nodes)
Yes, with Memcached (distributed cache) - Warning, when using an SQL database, this will disable the Hibernate 2nd lev
el cache!
❯ Yes, with the Redis implementation
No - Warning, when using an SQL database, this will disable the Hibernate 2nd level cache!
Yes, with the Ehcache implementation (local cache, for a single node)
Yes, with the Caffeine implementation (local cache, for a single node)
? Do you want to use Hibernate 2nd level cache? (Y/n)

After deciding which database should be used in the project it’s time for cache. JHipster provides options like: in-memory cache (Redis), local cache (EhCache, Caffeine), distributed cache (Hazelcast, Memcached) and hybrid cache (Infinispan). When cache engine is selected we have to enable or disable 2nd level cache in Hibernate.

? Would you like to use Maven or Gradle for building the backend? Maven
? Which other technologies would you like to use? (Press <space> to select, <a> to toggle all, <i> to invert selection)
❯◯ Search engine using Elasticsearch
◯ WebSockets using Spring Websocket
◯ Asynchronous messages using Apache Kafka
◯ API first development using OpenAPI-generator

With cache selected and configured, the next stop is the build management tool. I use Maven as I’m more familiar with this one. After that JHipster provides a list with additional technologies which should be configured in our project.

We are half way there. Our backend stack is configured and now we can focus on the frontend client. In the current version of JHipster two JavaScript frameworks are available: Angular and React. In the meantime the team is working on Vue.js integration so it might be available in the near future.

? Which *Framework* would you like to use for the client?
❯ Angular
React
No client
? Would you like to use a Bootswatch theme (https://bootswatch.com/)? Cosmo
? Choose a Bootswatch variant navbar theme (https://bootswatch.com/)? Dark
? Would you like to enable internationalization support? No

The last step is testing. This is a very convenient option because JHipster will provide configuration and example tests for selected frameworks.

? Besides JUnit and Jest, which testing frameworks would you like to use? (Press <space> to select, </space></i></a><i><a> to toggle all, <i> to invert selection)
❯◯ Gatling
◯ Cucumber
◯ Protractor

At this point JHipster should start generation of source code. After some time process will finish and command line will prompt that backend can be started with Maven wrapper and the front part is served by Webpack.

Run below command to build and run the application. Application will be available under http://localhost:8080/ URL.

./mvnw

By default two user accounts are available: admin/admin and user/user.

If your computer does’t have installed MySQL or Redis (like mine) it is possible to run them using docker (to avoid errors during application start-up). JHipster creates docker-compose files for each dependency required by our application. I had to run two commands from docker directory found in project tree.

docker-compose -f mysql.yml up -d
docker-compose -f redis.yml up -d

Now it’s a good time to load project to your favorite IDE and play around. SpringBoot Dev Tools are configured in maven dependency and most changes in the source code should trigger live reload.

What next?

At this point we have covered root functionality of JHipster, but it’s not all. As this post is getting pretty long I will briefly describe how to extend our project with data model.

JHipster generator allows to import a JDL file with model schema. JDL stands for JHipster-specific Domain Language. Below you can see an example of such a file.

entity Board {
    boardName String
}

entity Task {
    taskName String,
    taskDescription String,
    taskType TaskType
}

enum TaskType {
    TAKE_YOUR_TIME, ASAP
}

// defining multiple OneToMany relationships with comments
relationship OneToMany {
    Board to Task{board},
}

// Set pagination options
paginate Task with infinite-scroll

// Use Data Transfer Objects (DTO)
dto * with mapstruct

// Set service options to all
service all with serviceImpl

As the content of this file we can add our data model, define relations and metadata that will instruct JHipster how to interpret the content. To use the above example create a data-model.jdl file in the root directory of project and copy inside the above example. To convert model from JDL to actual classes execute this command:

jhipster import-jdl data-model.jdl

This will create database schema, migration scripts, POJO classes, services and controllers to execute basic CRUD operations. After rebuilding the application with ./mvnw command, and logging to admin account, entity section should contain Task and Board tabs.

Conclusion

JHipster is an easy and very robust tool. It supports many of currently popular technologies used in web application development. If not used for development it can be a good snippet how to configure and implement one of supported technologies in your main project. I found it a good source of knowledge how to approach integration in SpringBoot with many external libs in well-organized manner.

Links: https://www.jhipster.tech

Poznaj mageek of j‑labs i daj się zadziwić, jak może wyglądać praca z j‑People!

Skontaktuj się z nami