Introducing Spring Native for JHipster: Serverless Full-Stack Made Easy
Tags: java, spring, programming
Published: 2022-03-16 15:28:13

### The newly released JHipster Native blueprint allows you to generate Spring Boot projects with Spring Native support.

### <br>

Over the years, I’ve developed a lot of Java applications. I started writing Java code in the late 90s and spent several years doing Java before I tried another server-side language. I was impressed when I first tried building apps in Ruby on Rails, Python, and Node.js - they all started super-fast!

Starting fast is cool, but we in the Java community have often asked, does it perform over time? The Java Virtual Machine is famous for performance and optimization over time.

I scoffed at serverless when it first came out. Mostly because I was a Java developer, and my apps didn’t start in milliseconds. They also used a whole lotta memory, and there was no hope in sight.

Then, along came [GraalVM](https://www.graalvm.org/). It’s gained support from many Java frameworks in the last few years and has made their apps start in milliseconds!

Today, I’m proud to announce that this same capability is now available for your JHipster 7+ apps!

**Prerequisites**:

* [Java 17 with GraalVM](https://sdkman.io/)+
* [Docker Desktop](https://docs.docker.com/desktop/#download-and-install)

<br />

## Why Should You Care about Serverless?

From [IBM’s What is Serverless computing?](https://www.ibm.com/cloud/learn/serverless)

> Serverless is a cloud execution model that enables a simpler, more cost-effective way to build and operate cloud-native applications.

That’s it!

For companies with high traffic and large cloud bills, serverless makes sense. They can save millions of dollars a month.

## GraalVM Allows Serverless Java for Everyone!

I’ve been playing with GraalVM and the Java frameworks that support it for around 18 months. After doing a lot of research, I blogged about [building native Java apps with Micronaut, Quarkus, and Spring Boot](https://developer.okta.com/blog/2021/06/18/native-java-framework-comparison).

In September 2021, I did a talk with Josh Long about Spring Native with JHipster at the San Francisco JUG.

<NonAmpContent>
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Let the games begin with <a href="https://twitter.com/starbuxman?ref_src=twsrc%5Etfw">@starbuxman</a>! <a href="https://twitter.com/hashtag/Java?src=hash&amp;ref_src=twsrc%5Etfw">#Java</a> <a href="https://twitter.com/hashtag/SpringNative?src=hash&amp;ref_src=twsrc%5Etfw">#SpringNative</a> <a href="https://twitter.com/hashtag/JHipster?src=hash&amp;ref_src=twsrc%5Etfw">#JHipster</a> <a href="https://t.co/0KSRTd89e5">pic.twitter.com/0KSRTd89e5</a></p>&mdash; Matt Raible (@mraible) <a href="https://twitter.com/mraible/status/1442698739563446276?ref_src=twsrc%5Etfw">September 28, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
</NonAmpContent>

I flew out on a Monday; we got everything working by Wednesday afternoon and then spoke about our experience on Wednesday night. I wrote about it in [JHipster Works with Spring Native!](https://www.linkedin.com/pulse/jhipster-works-spring-native-matt-raible/) on LinkedIn and created a [JHipster issue](https://github.com/jhipster/generator-jhipster/issues/16498) to automate our learnings.

## What is Spring Native?

Spring Native provides an API to configure Spring Boot and GraalVM, so classes that are not easily discoverable become recognizable. For example, those that are instantiated using reflection. It’s a really slick extension to Spring Boot and will likely disappear with Spring Boot 3 because it’ll be native by default.

As a result of our successful [presentation at the SF JUG](https://youtu.be/F9oydL_MndA), Josh and I were invited to speak at the Garden State JUG in December. We laid low for a few months and rekindled our research in December. I wrote about it in [JHipster works with Spring Native, Part 2!](https://www.linkedin.com/pulse/jhipster-works-spring-native-part-2-matt-raible/)

## 🔥 Announcing the JHipster Native Blueprint!

In late January 2022, we (the JHipster team) upgraded JHipster to use Spring Boot 2.6 and released version [7.6.0](https://www.jhipster.tech/2022/01/23/jhipster-release-7.6.0.html).

In early February, I updated [the Spring Native with JHipster examples](https://github.com/mraible/spring-native-examples) that Josh and I’d been using for research. I was ready to start automating the Spring Native integration using a JHipster module. When [I asked the JHipster team](https://github.com/jhipster/generator-jhipster/issues/16498#issuecomment-1030263905) about the best way to implement it, [Marcelo Shima](https://github.com/mshima) volunteered to create the initial blueprint.

This was on a Friday afternoon (Mountain Time or MT). By the next morning, Marcelo had the MVP finished and provided steps to reproduce our examples. 😳

I was amazed! I remember telling (my partner) Trish that he’d done a week’s worth of (my estimated) work in a matter of hours.

Today, I’m proud to announce the [JHipster Native blueprint](https://github.com/jhipster/generator-jhipster-native) is available! Here’s how to use it:

```
npm install -g generator-jhipster-native@1.0
jhipster-native
```

This will generate a JHipster app and integrate GraalVM automatically. You can build and run a native image with the following commands:

```
./mvnw package -Pnative,prod -DskipTests
npm run ci:e2e:prepare # start docker dependencies
./target/native-executable
```

Gradle is not currently an option, but it can be if we add support to the JHipster Native blueprint.

## Go Native with Spring Native and GraalVM

To see Spring Native + JHipster in action, let’s look at a [previous JHipster app I created for the Auth0 blog](https://auth0.com/blog/full-stack-java-with-react-spring-boot-and-jhipster/). First, clone the example:

```
git clone https://github.com/oktadev/auth0-full-stack-java-example.git jhipster-native
cd jhipster-native
```

Want results right away? Clone the `spring-native` branch with the changes below already made:

```
git clone -b spring-native https://github.com/oktadev/auth0-full-stack-java-example.git jhipster-native
```

Then, skip to the [Configure your OpenID Connect identity provider](#Configure-Your-OpenID-Connect-Identity-Provider) section to continue.


Install JHipster 7.7.0 and the JHipster Native blueprint:

```
npm i -g generator-jhipster@7.7
npm i -g generator-jhipster-native@1.0
```

Then, remove all the existing project files and regenerate them. The `jhipster-native` command includes parameters to disable caching because it’s not supported by Spring Native yet.

```
rm -rf *
jhipster-native --with-entities --cache-provider no --no-enable-hibernate-cache
# When prompted to overwrite .gitignore, type "a" to overwrite all files
```

If you open the project in IntelliJ IDEA, you can use the **Commit Tools Window** (⌘+0 on macOS or Ctrl+0 on Linux/Windows) to view files that changed.

![JHipster](https://images.ctfassets.net/23aumh6u8s0i/6WXqAwFxjy0G0983LRBonL/fa7aa51acef38b8d26918622b495cfba/01-commit-tools-window.jpeg)

Next, run the following `git checkout` commands to restore the files that were modified in the original example.

```
git checkout .gitignore
git checkout README.md
git checkout demo.adoc
git checkout flickr2.jdl
git checkout screenshots
git checkout src/main/webapp/app/entities/photo/photo.tsx
git checkout src/main/webapp/app/entities/photo/photo-update.tsx
git checkout src/main/java/com/auth0/flickr2/config/SecurityConfiguration.java
git checkout src/main/resources/config/application-heroku.yml
git checkout src/main/resources/config/bootstrap-heroku.yml
git checkout Procfile
git checkout system.properties
```

If you’d rather not use the command line, you can right-click on each file and select **Rollback**.

![JHipster](https://images.ctfassets.net/23aumh6u8s0i/1qtRcpu4RZIK3bmyW4Iijr/5a9df8c695d23e3cd01a13eba0867d44/02-rollback-commit.png)

[Read more...](https://auth0.com/blog/spring-native-jhipster/?utm_source=content_synd&utm_medium=sc&utm_campaign=spring)