Building a Spring application with Quarkus

In the last post, we have created a simple Quarkus application. For those who are familiar with Spring it is better to code in their way. Luckily, Quarkus supports Spring out of box.

Hantsy
Hantsy
Nov 21, 2019 · 5 min read

There are some Quarkus extensions available to support Spring framework.

  • spring-di — Spring core framework
  • spring-web — Spring WebMVC framework
  • spring-data — Spring Data JPA integration

In this post, we will create a Quarkus application with similar functionality in the last post but here we are using the Spring extensions.

Generate a Quarkus project skeleton

Similarly, open your browser and navigate to Starting Coding page.

  1. Input spring in the Extensions text box to filter the extensions.
  2. Select all Spring related extensions, and customize the value of group and artifactId fields as you like.
  3. Hit the Generate your application button or use the keyboard shortcuts ALT+ENTER to produce the project skeleton into an archive for downloading.
  4. Download the archive file, and extract the files into your disk, and import them into your favorite IDE.

Next, we’ll add some codes to experience the Spring related extensions.

Enabling JPA Support

First of all, you need to configure a DataSource for the application.

Using quarkus:list-extensions goal to list all extensions provided in Quarkus, there are a few jdbc extensions available.

Let’s use PostgresSQL as an example, and add the jdbc-postgresql extension into the project dependencies.

Open your terminal, execute the following command in the project root folder.

Finally, a new quarkus-jdbc-postgresql artifact is added in the pom.xml file.

Let’s reuse the Post entity we created in the last post, and create a Repository for this Post entity.

Creating a Spring Data specific Repository

The following is an example of PostRepository. JpaRepository is from Spring Data JPA project which provides common operations for JPA.

Currently it seems only the basic Repository is supported, a lot of attractive features are missing in the current Quarkus Spring Data support, including:

  • QueryDSL and JPA type-safe Criteria APIs, see #4040
  • Custom Repository interface, see #4104, #5317, fixed in 1.0.0.CR2.

Create a custom interface PostReposiotryCustom.

Make PostRepository to extend PostRepositoryCustom.

Provides a implementation for PostReposiotryCustom.

The findByKeyword method uses JPA Criteria APIs to filter posts by keyword, and also paginated the result by offset and limit parameter.

Creating a RestController

Create a @RestController to expose Post resources.

Currently, there are some limitation when creating a RestController.

  • The return type does not support Page, see #4056 fixed.
  • The request parameter @PageableDefault Pageable is not supported, see #4041

Handling Exceptions

In the getPost method of the RestController class, there is a PostNotFoundException thrown when a post is not found , let's create a ControllerAdvice to handle it .

There are some limitations here.

  • In Quarkus, a @ExceptionHandler can only be used in the RestControllerAdvice class. @ExceptionHandler method in controllers is not supported now.
  • @ExceptionHandler method can not accept Spring specific parameters, see #4042. E.g. if you want to access the HTTP request, try to replace the Spring favored WebRequest with the raw Servlet based HttpServletRequest.

To use the Servlet APIs, you have to quarkus-undertow into the project dependencies.

<dependency>

<groupId>io.quarkus</groupId>

<artifactId>quarkus-undertow</artifactId>

</dependency>

Run the application

Execute the following command to build and run the application.

After it is started, try to access the APIs using curl.

As you see, there are some issues in the JSON serialization.

  1. The JSON format is not good to read.
  2. The datetime format is serialized as an array of timestamps numbers.

To customize the JSON serialization, like we do in Spring application development, just customize a Jackson ObjectMapper.

Quarkus does not provides a Spring Boot Customizer like tool to customize Jackson ObjectMapper, but you can declare a ObjectMapper bean in your @Configuration class to archive the purpose like you do in before Spring applications.

Save the work, and run the application again. Try to access the http://localhost:8080/posts.

Get the source codes from my Github.

The Startup

Get smarter at building your thing. Join The Startup’s +800K followers.

Hantsy

Written by

Hantsy

Self-employed technical consultant, solution architect and full-stack developer

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Hantsy

Written by

Hantsy

Self-employed technical consultant, solution architect and full-stack developer

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +800K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store