27 January 2017

JHipster - Spring Code Generation

A month and a bit ago, I wrote about Ruby on Rails, comparing some nice features it has over the lack of similar Spring features.

Specifically, application structure/code generation, and automated database migrations.

While it's still true that you get a lot of features baked in to vanilla Rails that you just don't get with Spring, I've recently discovered a bunch of Spring projects that provide these features.

Probably one of the most extreme/exciting is JHipster, which is a tool that brings together loads of other projects in a easy and consistent way.

JHipster

I can't say I'm too fond of the name, but I can't deny that it is very cool.

Put simply, JHipster is a tool that generates production-quality Java Spring web applications quickly and easily. It uses the latest (stable) technologies and best practices, and eliminates the need to manually add standard boilerplate code and configuration.

It uses a nice Question and Answer process when generating new projects via the commandline, for example:


As you can see, you can choose different types of project to generate; monolithic, microservice, or microservice gateway, which makes a big difference to the structure and technologies used. If I were to generate a completely default JHipster project, this is what I'd get:

  • Monolithic Spring Boot application
  • Embedded lightweight Undertow webserver, with development/production configuration, auto-refresh support in development, automatic css/js optimisation and minification in production
  • Responsive AngularJS frontend, with Spring monitoring and healthcheck interfaces
  • Standard Logback usage (with management interfaces)
  • Spring Security HTTP Session authentication with provided admin/user users, with profile and password reset facility
  • MySQL production database configuration, with H2 in-memory development database configuration
  • Hibernate support, with configured 2nd level cache
  • Liquibase integration
  • Swagger integration
  • ehcache database caching
  • Internationalisation support
  • Gatling load testing integration
  • Docker configuration already generated
  • Local Maven, with build and plugin configurations

There's probably a lot more too. If you were to run this app (just by executing "mvnw" in the project root), you'd see this at localhost:8080:


You can now try logging in, updating the profile, changing their password, browsing the monitoring of the application, changing log levels, and more. However, there's currently no data, so you'd need to generate some Entities (the Spring equivalent term of Rails' "Model") using one of many provided "sub-generators":


(You don't have to go through this every time to add or change things if you don't want to.)

You're asked about all the fields you'd like to add to the new entity, and any validation required on them. It'll also ask if you want to generate any relationships with the new entity, and what it is. When you're done, JHipster will generate the required Java classes, AngularJS frontend for display and management, ehcache configuration, unit tests, and Liquibase configurations.

All you now need to do is run your application again, and the new Liquibase configs will be detected, and database changes made automatically. This is very similar to "rails db:migrate", only it happens automatically!

Now, you have a new database table, migrations, data objects, relationships, a suite of REST interfaces, display and administration, pagination implementation (if requested), and unit tests, without writing a single line of code :).

JHipster can do so much more than this too. You can easily break up a monolith into microservices, with automatic service discovery and load balancing. You can choose different technologies to use, like authentication types and database types. It's also clever about performing upgrades and generations on manually modified code.

I admit that part of me is a little uncomfortable about so much being done for you. I come from a bit of an old-school world where I like knowing every nook and cranny of a project. It took me a while to get comfortable using non-standard libraries! However, it does make sense to automate the boring boilerplating process, so we can get to the business logic (and the fun stuff) more quickly.

I will certainly be exploring this more, as it does seem to be the modern way of doing things, and I imagine the slight discomfort will pass once I'm more familiar with it all. After all, there's no reason to reinvent the wheel each time, is there?

No comments:

Post a Comment