Spring, Quarkus and Java records

Remi Forax forax at univ-mlv.fr
Wed Jan 5 23:49:52 UTC 2022


Hi all,
I've cut and paste several tutorials explaining how to use Spring Boot and Quarkus (the RedHat metoo) to implement a REST app server
and try to see where it can be interesting to use Java records instead of classical Java classes.

For me, records can be used at two locations,
- as parameter/return type of the REST controller method to decode/encode JSON, so users have a better control on the JSON emitted.
  It's important because it becomes easy to filter out a key and to control the order of the keys of the JSON objects.
- a the REST controller itself, because it forces the user to only use constructor injection (and not setter/field injection)
  and it also avoid user to create fields in the controller forgetting to add the proper synchronization.

For Spring,
- Obviously, a JPA entity can not be a record because JPA/Hibernate requires an entity to be mutable.
- Jackson supports records, so parameter and return type of the REST controller methods can use records.
- Sadly, declaring a REST controller as a record is not supported, because the implementation of @Transactionnal creates a subclass.
  
For Spring reactive,
- Spring Reactive do not use JPA or Reactive Hibernate but R2DBC, and R2DBC supports records so mutable entities anymore, yai !

For Quarkus,
- like with Spring, Quarkus uses Jackson so the parameter and return type of the methods of the REST controller can use records.
- the controller can be a record too, Quarkus change the bytecode to support @Transactional, so no subclass needed.

For Quarkus reactive,
Quarkus does not provide any reactive implementation for embedded databases like H2, so i was not able to test properly that combination.

The code is here:
https://github.com/forax/webapp-java17

regards,
Rémi

PS: I also know that JOOQ and Jdbi supports records.


More information about the amber-dev mailing list