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:
./mvnw quarkus:add-extension -D"extensions=spring-web,spring-data-jpa"
quarkus ext add spring-web spring-data-jpa
[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:3.10.2: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: 2024-05-23T21:33:15-04:00
[INFO] ------------------------------------------------------------------------
Notice in the logs how Quarkus is reloading and the Spring compatibility extensions are now part of the Installed features
.
Create a Spring Data Repository for Movie
Spring Data is one of the most popular Spring APIs, so let’s create a Spring Data JPA Repository for our Movie
entity. Create the SpringMovieRepository
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 SpringMovieRepository extends JpaRepository<Movie, Long> {
public List<Movie> findByYear(String year);
}
Create a Spring REST Controller
Now let’s create another REST endpoint for Movie
, but now using the Spring Web APIs. Create the MovieController
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-movie")
public class MovieController {
private SpringMovieRepository movieRepository;
public MovieController(SpringMovieRepository movieRepository) {
this.movieRepository = movieRepository;
}
@GetMapping
public List<Movie> movies(@RequestParam("year") String year) {
if (year != null) {
return movieRepository.findByYear(year);
}
return movieRepository.findAll();
}
}
Let’s try to filter only the movies with year 1980. Don’t forget to start Quarkus dev mode again if you stopped it.
curl -w '\n' localhost:8080/spring-movie?year=1980
[
{
"id": 2,
"title": "The Empire Strikes Back",
"releaseDate": "1980-05-17"
}
]
You’ll see that the behavior is the same as the one provided with JAX-RS and Hibernate with Panache.