{{announcement.body}}
{{announcement.title}}

Migrating SpringBoot PetClinic REST to Quarkus

DZone 's Guide to

Migrating SpringBoot PetClinic REST to Quarkus

In this post, I'm going to cover my experience migrating the Spring Boot PetClinic REST application to Quarkus.

· Java Zone ·
Free Resource

In this post, I'm going to cover my experience migrating the Spring Boot PetClinic REST application to Quarkus, to check how hard it can be and also to see which are the metrics I obtain at the end.

This is only an experiment with an application with low complexity but touching several common libraries and concepts used by anyone that creates a Spring Boot application.

Motivation

My experience developing applications using the SpringBoot framework has been:

  • Tests last ages
  • Artifacts generated are big
  • Applications consume lots of resources
  • So many things happening under the hood
  • Services started without request
  • Easy to code as the framework makes things simpler
  • Private company opensource solution

And I read about Quarkus and it is:

  • Easy to code
  • Focused on GraalVM compilation
  • Opensource standard libraries used
  • Fast and light

So, I decided to check those claims migrating a simple project that uses SpringBoot to see if it's easy and production-ready the mind migration for someone very used to code using SpringBoot.

Context

SpringBoot PetClinic REST project, GitHub repository: https://github.com/spring-petclinic/spring-petclinic-rest

Quarkus migrated project, GitHub repository: https://github.com/jonathanvila/spring-petclinic-rest/tree/quarkus

Modules

Java
 




x
23


 
1
SpringBoot PetClinic REST
2
 
             
3
Quarkus PetClinic REST
4
 
             
5
Spring Data ( repositories, jdbc , ... )
6
Spring Web ( REST )
7
Spring Security
8
Spring documentation ( Swagger )
9
Spring actuators
10
Spring micrometer
11
Spring CDI
12
Spring AOP
13
Spring cache
14
 
             
15
Hibernate Panache
16
JaxRS
17
Quarkus Security
18
OpenAPI
19
SmallRye Health
20
Microprofile Metrics
21
CDI Spec ( Arc )
22
-
23
Quarkus cache ( Caffeine )



Elements NOT Migrated

  • Spring JDBC Querying
    • No helper methods to work with Inserts, Updates
    • No equivalent to org.springframework.jdbc.core classes
    • We need to reimplement everything using AGROAL
  • JMX
    • Not supported by GraalVM definition

There are two ways of migrating the application:

  1. Option A: moving from Spring to standard libraries
  2. Option B: keep using Spring interfaces and not touch your code

Migration Steps Option A: to Standard libraries

This option involves moving from Spring API interfaces into new libraries as JAX-RS, Panache, Microprofile, Hibernate validation, Quarkus Security.

REST

Move from Spring REST to the standard JAX-RS annotations.

  1. @RestController --> @Path
  2. @RequestMapping --> break it down into next annotations
  3. @GetMapping --> @GET
  4. @PostMapping --> @POST
  5. @PutMapping --> @PUT
  6. @DeleteMapping -> @DELETE
  7. @PathVariable -> @PathParam

@path code

Security

  1. Replace
  • @PreAuthorize("hasRole(@roles.ROLE_ADMIN)") by 
  • @RolesAllowed("ROLE_ADMIN")
@RolesAllowed

2. Add Elytron extension to persist security in the database: quarkus-elytron-security-JDBC extension.

3. Configure the extension on properties.file

security

CORS

  1. Configured CORS in the application.properties file
quarkus

Metrics

  1. Add smallRye metrics extension
  2. Annotate each method (for custom metrics)
  3. Enable Hibernate metrics in the properties file
@Override
hibernate

Validation

  1. Spring Validation to Hibernate Validator
  2. Move from @ControllerAdvice to JAX-RS ExceptionMapper

OpenAPI Doc

  1. Add extension "openapi"
  2. Extend Application class
@OpenAPIDefinitions

Local Caching

  1. Spring uses a default ConcurrentHashMap
  2. Caffeine uses a ConcurrentLinkedHashMap
  3. Add "cache" extension ( Caffeine ) and annotate the method
  4. Configure each cache behavior in the properties file
@Transactional
#Cache

Migration Steps — Option B: Using Spring Interfaces

This option involves using the spring quarkus extensions, and not change your code if you are not using a not supported feature.

There are features not supported in some of the Spring extensions, so better check them if you want to use this path.

REST

rest controller


  1. Add spring-web extension

CDI

CDI


  1. Add spring-di extension

JPA

JPA


  1. Add spring-data-jpa extension

Security

security


  1. Add spring-security extension

Testing

Test REST

Test Resources

  1. Annotate test suites with @QuarkusTest to boot the app
  2. Annotate test suites with @QuarkusTestResource to load an embedded resource

Test Mock

  1. Use the quarkus-mockito dependency: quarkus-junit5-mockito
  2. Annotate mocks with @InjectMock

Performance Comparative


Spring Boot Quarkus Hotspot Quarkus GraalVM
BUILD 48 MB
4.3 s
42 MB
13 s
97 MB
185 s
BOOT 630 MB
6.8 s
251 MB
2.6 s
21 MB
0.462 s

References

Presentation at DevNexus

Video: https://www.youtube.com/watch?v=BHuQ_9jATk0&list=PLid93BOrASLPJG2IP3rVENiPAzpVo0G3Q&index=40

Slides: https://tiny.cc/sb2qks

Documentation/News/Help

  1. https://quarkus.io
  2. https://developers.redhat.com/search/?t=quarkus
  3. quarkus-dev@googlegroups.com

Interactive Tutorials

https://developers.redhat.com/courses/quarkus/

Cheat Sheet

https://lordofthejars.github.io/quarkus-cheat-sheet/

To Start Coding With Quarkus

https://code.quarkus.io/

Red Hat MTA (Migration Toolkit for Applications)

This is an opensource tool to help migrate an application from different sources to different targets :

  1. Apache Camel 2 to Apache Camel 3
  2. Oracle WebLogic and IBM WebSphere to JBoss EAP
  3. Oracle JDK to OpenJDK
  4. On-Prem to Cloud and container readiness
  5. Spring Boot to Quarkus

It consists of a rules engine and several rules ( > 1500 rules ) to statically analyze Java applications (packaged or source code) and give hints of changes to apply to move the application from the source to the target.

https://developers.redhat.com/products/mta/overview https://github.com/windup
application list
Issues
Topics:
graalvm, java, microprofile, native, quarkus, rest, springboot, tutorial

Published at DZone with permission of Jonathan Vila . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}