Difficulty: beginner
Estimated Time: 15 minutes

This exercise demonstrates how Spring and Spring Boot developers can use well-known Spring annotations for Spring Data, Web, and Dependency Injection when building Quarkus applications. Spring developers can quickly become productive with Quarkus using existing knowledge and familiarity of these APIs.

You'll build a relatively simple application that uses Spring Data to integrate (via JPA) with an underlying database, inject beans using Spring DI and expose them as RESTful endpoints via Spring Rest.

Other possibilities

Learn more at quarkus.io, or just drive on and get hands-on!

This exercise demonstrated how Spring and Spring Boot developers can use well-known Spring annotations for Spring Data, Web, and Dependency Injection when building Quarkus applications. Spring developers can quickly become productive with Quarkus using existing knowledge and familiarity of these APIs.

You built a relatively simple application that uses Spring Data to integrate (via JPA) with an underlying database, inject beans using Spring DI and expose them as RESTful endpoints via Spring Rest.

NOTE Please note that the Spring support in Quarkus does not start a Spring Application Context nor are any Spring infrastructure classes run. Spring classes and annotations are only used for reading metadata and / or are used as user code method return types or parameter types. What that means for end users, is that adding arbitrary Spring to the classpath will not be used at all. Moreover Spring infrastructure things like org.springframework.beans.factory.config.BeanPostProcessor will not be run.

Additional Resources

If you’re interested in helping continue to improve Quarkus, developing third-party extensions, using Quarkus to develop applications, or if you’re just curious about it, please check out these links:

Quarkus for Spring Boot Developers

Step 1 of 6

Step 1

Inspect Java runtime

An appropriate Java runtime has been installed for you. Ensure you can use it by running this command:

If the command fails, wait a few moments and try again (it is installed in a background process and make take a few moments depending on system load).

$JAVA_HOME/bin/java --version

The command should report the version in use, for example (the versions and dates may be slightly different than the below example):

openjdk 11.0.10 2021-01-19
OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.10+9)
OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.10+9, mixed mode)

Create basic project

Let's create the basic Quarkus Hello World application and include the necessary spring extensions. Click this command to create the project:

cd /root/projects/quarkus && mvn io.quarkus:quarkus-maven-plugin:2.0.0.Final:create \ -DprojectGroupId=org.acme \ -DprojectArtifactId=fruit-taster \ -Dextensions="spring-data-jpa,spring-web,spring-di,jdbc-postgres, jdbc-h2"

This will use the Quarkus Maven Plugin and generate a basic Maven project for you in the fruit-taster subdirectory and include the following extensions to enable Spring annotations and APIs:

  • spring-data-jpa - Adds Spring Data annotations like @CrudRepository allowing integration with database-backed JPA repositories
  • spring-web - Adds Spring Web annotations like @RestController, @RequestMapping, @PathVariable, @GetMapping, etc
  • spring-di - Adds Spring DI annotations like @Autowired, @Configuration, @Component, etc
  • jdbc-postgres - Driver for Postgresql database. Note this is the jdbc variant. Reactive programmers may be interested in the Reactive Postgres Client.
  • jdbc-h2 - We also use the h2 database for local development

Start the app

First, switch to the directory in which the app was built:

cd /root/projects/quarkus/fruit-taster

Let's begin Live Coding. Click on the following command to start the app in Live Coding mode:

mvn quarkus:dev -Dquarkus.http.host=0.0.0.0

You should see:

__  ____  __  _____   ___  __ ____  ______
 --/ __ \/ / / / _ | / _ \/ //_/ / / / __/
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ /\ \
--\___\_\____/_/ |_/_/|_/_/|_|\____/___/
INFO  [io.quarkus] (Quarkus Main Thread) fruit-taster 1.0.0-SNAPSHOT on JVM (powered by Quarkus xx.xx.xx.Final) started in xxxs. Listening on: http://0.0.0.0:8080
INFO  [io.quarkus] (Quarkus Main Thread) Profile dev activated. Live Coding activated.
INFO  [io.quarkus] (Quarkus Main Thread) Installed features: [agroal, cdi, hibernate-orm, hibernate-orm-panache, jdbc-h2, jdbc-postgresql, narayana-jta, resteasy, resteasy-jackson, smallrye-context-propagation, spring-data-jpa, spring-di, spring-web]

--
Tests paused, press [r] to resume, [h] for more options>

The first time you build the app, new dependencies may be downloaded via maven. This should only happen once, after that things will go even faster.

Test that the app is running by accessing the simple hello endpoint:

cd /root/projects/quarkus/fruit-taster && \ curl http://localhost:8080/greeting

You may need to click this command again in case it doesn't execute properly on first click

you should see

Hello Spring

This app doesn't use any Spring annotations yet. You'll live code those in the next few steps while the app continues to run.