Spring Compatibility

Are you a Spring Framework user? Wouldn’t it be wonderful if you could reuse your acquired Spring skills with live coding, small footprints, and fast time-to-first-response? With Quarkus, you don’t have to give up one for the other: you can have it all!

Adding the Spring Compatibility Extensions

Quarkus supports many of your favorite Spring Framework projects. In this chapter, we’re going to use the Spring DI, Spring Web, and Spring Data APIs. Let’s add the required extensions:

  • Maven

  • Quarkus CLI

./mvnw quarkus:remove-extension -D"extensions=quarkus-resteasy-reactive"
./mvnw quarkus:add-extension -D"extensions=quarkus-spring-web,quarkus-spring-data-jpa,quarkus-resteasy-reactive-jackson"
quarkus ext remove quarkus-resteasy-reactive
quarkus ext add quarkus-spring-web quarkus-spring-data-jpa quarkus-resteasy-reactive-jackson
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------< com.redhat.developers:tutorial-app >-----------------
[INFO] Building tutorial-app 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- quarkus-maven-plugin:1.4.2.Final:add-extension (default-cli) @ tutorial-app ---
✅ Adding extension io.quarkus:quarkus-spring-data-jpa
✅ Adding extension io.quarkus:quarkus-spring-web
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.052 s
[INFO] Finished at: 2020-05-11T21:33:15-04:00
[INFO] ------------------------------------------------------------------------

The quarkus-spring-web extension requires quarkus-resteasy-reactive-jackson. In order to be conservative with the amount of dependencies, the quarkus-resteasy-reactive was removed in favor of quarkus-resteasy-reactive-jackson.

Create a Spring Data Repository for Fruit

Spring Data is one of the most popular Spring APIs, so let’s create a Spring Data JPA Repository for our Fruit entity. Create the SpringFruitRepository Java class in src/main/java in the com.redhat.developers package with the following contents:

package com.redhat.developers;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SpringFruitRepository extends JpaRepository<Fruit, Long> {

    public List<Fruit> findBySeason(String season);

}

Create a Spring REST Controller

Now let’s create another REST endpoint for Fruit, but now using the Spring Web APIs. Create the FruitController Java class in src/main/java in the com.redhat.developers package with the following contents:

package com.redhat.developers;

import java.util.List;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(path = "/spring-fruit")
public class FruitController {

    private SpringFruitRepository fruitRepository;

    public FruitController(SpringFruitRepository fruitRepository) {
        this.fruitRepository = fruitRepository;
    }

    @GetMapping
    public List<Fruit> fruits(@RequestParam("season") String season) {
        if (season != null) {
            return fruitRepository.findBySeason(season);
        }
        return fruitRepository.findAll();
    }

}

Let’s try to filter only the fruits with the Summer season. Don’t forget to start quarkus dev mode again if you stopped it.

curl localhost:8080/spring-fruit?season=Summer
[
  {
    "id": 5,
    "name": "Blueberry",
    "season": "Summer"
  },
  {
    "id": 6,
    "name": "Banana",
    "season": "Summer"
  },
  {
    "id": 7,
    "name": "Plum",
    "season": "Summer"
  }
]

You’ll see that the behavior is the same as the one provided with JAX-RS and Hibernate with Panache.