DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • The Most Popular Technologies for Java Microservices Right Now
  • Micronaut With Relation Database and...Tests
  • How to Introduce a New API Quickly Using Micronaut
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era

Trending

  • Navigating Change Management: A Guide for Engineers
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques
  • Using Python Libraries in Java
  • Manual Sharding in PostgreSQL: A Step-by-Step Implementation Guide
  1. DZone
  2. Coding
  3. Java
  4. Tackling Records in Spring Boot

Tackling Records in Spring Boot

Let’s take a look at several scenarios where Java records can help us increase readability and expressiveness by squeezing the homologous code.

By 
Anghel Leonard user avatar
Anghel Leonard
DZone Core CORE ·
May. 21, 24 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
4.8K Views

Join the DZone community and get the full member experience.

Join For Free

Java records fit perfectly in Spring Boot applications. Let’s have several scenarios where Java records can help us increase readability and expressiveness by squeezing the homologous code.

Using Records in Controllers

Typically, a Spring Boot controller operates with simple POJO classes that carry our data back over the wire to the client. For instance, check out this simple controller endpoint returning a list of authors, including their books:

Java
 
@GetMapping("/authors")
public List<Author> fetchAuthors() {
  return bookstoreService.fetchAuthors();
}


Here, the Author (and Book) can be simple carriers of data written as POJOs. But, they can be replaced by records as well. Here it is:

Java
 
public record Book(String title, String isbn) {}
public record Author(String name, String genre, List<Book> books) {}


That’s all! The Jackson library (which is the default JSON library in Spring Boot) will automatically marshal instances of type Author/Book into JSON. In the bundled code, you can practice the complete example via the localhost:8080/authors endpoint address.

Using Records With Templates

Thymeleaf is probably the most used templating engine in Spring Boot applications. Thymeleaf pages (HTML pages) are typically populated with data carried by POJO classes, which means that Java records should work as well.

Let’s consider the previous Author and Book records, and the following controller endpoint:

Java
 
@GetMapping("/bookstore")
public String bookstorePage(Model model) {
  
  model.addAttribute("authors",
  bookstoreService.fetchAuthors());
  
  return "bookstore";
}


The List<Author> returned via fetchAuthors() is stored in the model under a variable named authors. This variable is used to populate bookstore.html as follows:

HTML
 
...
<ul th:each="author : ${authors}">
  <li th:text="${author.name} + ' (' + ${author.genre} + ')'" />
  <ul th:each="book : ${author.books}">
    <li th:text="${book.title}" />
  </ul>
</ul>
...


Done! You can check out the application Java Coding Problems SE.

Using Records for Configuration

Let’s assume that in application.properties we have the following two properties (they could be expressed in YAML as well):

Properties files
 
bookstore.bestseller.author=Joana Nimar
bookstore.bestseller.book=Prague history


Spring Boot maps such properties to POJO via @ConfigurationProperties. But, a record can be used as well. For instance, these properties can be mapped to the BestSellerConfig record as follows:

Java
 
@ConfigurationProperties(prefix = "bookstore.bestseller")
public record BestSellerConfig(String author, String book) {}


Next, in BookstoreService (a typical Spring Boot service), we can inject BestSellerConfig and call its accessors:

Java
 
@Service
public class BookstoreService {
  
  private final BestSellerConfig bestSeller;

  public BookstoreService(BestSellerConfig bestSeller) {
    this.bestSeller = bestSeller;
  }
  
  public String fetchBestSeller() {
     return bestSeller.author() + " | " + bestSeller.book();
  }
}


In the bundled code, we have added a controller that uses this service as well.

Record and Dependency Injection

In the previous examples, we have injected the BookstoreService service into BookstoreController using the typical mechanism provided by SpringBoot – dependency injection via constructor (it can be done via @Autowired as well):

Java
 
@RestController
public class BookstoreController {
  
  private final BookstoreService bookstoreService;
  
  public BookstoreController(BookstoreService bookstoreService) {
    this.bookstoreService = bookstoreService;
  }
  
  @GetMapping("/authors")
  public List<Author> fetchAuthors() {
    return bookstoreService.fetchAuthors();
  }
}


But, we can compact this class by re-writing it as a record as follows:

Java
 
@RestController
public record BookstoreController(BookstoreService bookstoreService) {
  
  @GetMapping("/authors")
  public List<Author> fetchAuthors() {
    return bookstoreService.fetchAuthors();
  }
}


The canonical constructor of this record will be the same as our explicit constructor. The application is available on GitHub.

Feel free to challenge yourself to find more use cases of Java records in Spring Boot applications.

Dependency injection Java (programming language) Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • The Most Popular Technologies for Java Microservices Right Now
  • Micronaut With Relation Database and...Tests
  • How to Introduce a New API Quickly Using Micronaut
  • Java's Quiet Revolution: Thriving in the Serverless Kubernetes Era

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!