Java World: Quarkus with Spring Boot
The Java ecosystem remains a frequent corporate choice for new projects where technological maturity is important. Perhaps the number of companies known as startups that start projects with Java is not so high, but certainly the platform remains a frequent choice in new projects of large corporations that seek innovation and that seek to do what is considered “digital transformation”.
But let’s be honest, some frameworks that have evolved into complete ecosystems have helped the Java language to stay on top of the most used technologies in the world.
In the new world of microservices, Spring Boot managed to keep Java very active in several projects of the most varied types of business, finance, e-commerce, retail…
But competition has also increased, as cloud provider costs are now a reality for companies to face. Languages that consume less computational resources will always be considered and even with the excellence of Spring, Java did not have that much advantage in this respect.
As the ecosystem and the Java community seem to have some good antifragility level, the problems and possible disadvantages of the Java platform were one of the main reasons for the creation of Quarkus.
So if you have read about how efficient Quarkus can be but like me, you know the countless qualities and enjoy working with Spring Boot, let’s see here how we can use the way of programming with Spring, using the efficiency of Quarkus.
This integration between Quarkus and Spring Boot is still in its initial phase, it is up to us developers to monitor the Quarkus project to see if there will be any evolution in this approach.
The example project:
A simple API Rest with GET and POST methods using Quarkus, but with Spring Boot’s annotations and design.
First: You need the Java SDK 11 (or OpenJDK) and Maven ready to use in your computer.
In your browser, access:
Look for the Compatibility extensions and select Spring DI and Spring Web API.
Example in the image below:
Click the Generate Your Application button. This will generate a .zip file with the initial code for your sample application.
Open or import the maven code into your favorite IDE.
After import your project on IDE, you have a Java class/file SpringGreetingController.java
You can modify this file with the code below:
This code is an example, there is not a database then our method save() and find() is only a “mock”.
Now, you can create a new class GreetingService.java with the code below:
In the root directory of your application you can execute the following command:
mvn compile quarkus: dev
Testing the application working:
Access http://localhost:8080 in your browser:
Testing GET and POST methods (with Postman or other tool of your preference):
GET (path: http://localhost:8080/quarkus-spring):
POST (path: http://localhost:8080/quarkus-spring):
We finished. If you already know the basics of Spring Boot, you certainly recognized the annotations that were used for the creation of the simple API Rest and for Dependency Injection. However, this is not a Java application with Spring, but Quarkus.
Quarkus also offers the use of Spring annotations such as:
Spring Data JPA, Spring Cloud Config Client, Spring Security and others.