Spring Boot and Java 16 Records

In this article, we will discuss Java 16’s newest feature, Records. Then we will apply this knowledge and use it in conjunction with a Spring Boot application.

Photo by Jan Kopřiva on Unsplash

Introduction

On March 16th, 2021, Java 16 was GA. With this new release, tons of new exciting features are added. Check out the release notes to know more about these changes in detail. This article’s focus will be on Java Records, which got delivered with JEP 395. Records were first introduced in JDK 14 as a preview feature proposed by JEP 359, and with JDK 15, they remained in preview with JEP 384. However, with JDK 16, Records are no longer in preview.

I have picked records because they are definitely the most favored feature added in Java 16, according to this Twitter poll by Java Champion Mala Gupta.

I also conducted a similar survey, but it was focused on features from Java 8 onwards. The results were not unexpected as Java 8 is still widely used. Very unfortunate, though, as tons of new features and improvements are added to newer Java versions. But in terms of features, Java 8 was definitely a game-changer from a developer perspective.

For some more Java and tech-related stuff, follow me on Twitter

So let’s discuss what’s the fuss is about Java Records.

What are Records?

As per the JEP 395:

Records are a new kind of class in the Java language. They act as transparent carriers for immutable data with less ceremony than normal classes. Records can be thought of as nominal tuples.

Another quote from the JEP clearly explains developers’ frustration while writing typical data carrier classes.

Properly writing such a data-carrier class involves a lot of low-value, repetitive, error-prone code: constructors, accessors, equals, hashCode, toString, etc. For example, a class to carry x and y coordinates inevitably ends up like this:

https://medium.com/media/09e6d929afb7a8e45773f46c7fd37b24/href

Another option that we developers use most often is to leave the handling of the boilerplate to IDE. For example, with Intellij, you can generate constructors, getters, setters, equals, hashCode, and toString, etc., by simply pressing Command + N shortcut key. But the boilerplate code is still there.

Auto-generate code with IntelliJ

With Java 16 Records, it’s just one line of code. Cool, isn’t it.

record Point(int x, int y) { }

Here a record class declaration consists of a name, optional type parameters, a header, and a body.

Demystifying Records

Internals of the Java record class can be checked using a decompiler that comes with IntelliJ IDE or can use the javap command-line utility. To understand the internals, we created the following record class.

public record State(String name, String capital) {}

Following is the decompiled Java record class. I have used the javap command-line utility to check class file internals.

$ javap State.class

Following is the output.

Compiled from "State.java"
public final class com.example.indianstates.State extends java.lang.Record {
public com.example.indianstates.State(java.lang.String, java.lang.String);
public final java.lang.String toString();
public final int hashCode();
public final boolean equals(java.lang.Object);
public java.lang.String name();
public java.lang.String capital();
}

You can conclude the following from the above output.

  1. State record class is extending the java.lang.Record abstract class.
  2. State record class is declared final cannot be extended further using extends keyword.
  3. hashCode(), equals(), toString() and a canonical constructor are implicitly generated for us.
  4. There are no setters or getters, only accessors.
  5. With no setters and final in the class declaration clarifies that you cannot change the state, and hence records are immutable.

You can further validate these points by writing tests as well.

https://medium.com/media/d73f0a8487efb906bff4b2a2ce3a8b47/href

There are some restrictions in the declaration of record classes as compared to normal classes. Checkout JEP 395 for such restrictions.

Lombok and Records, Friends or Foe?.

You are probably already using Lombok annotations such as @ Value, which is closest if not the same to the Java records. Then you can get rid of one dependency and those Christmas trees of annotations. I might have oversimplified things, and it may make sense to replace Lombok for some cases. But you might be using Lombok for other features and not just one annotation that it provides. And believe me, while Java Records are a welcome feature for Java lovers, but it’s not going to replace Lombok, at least for now. You do not believe me? Check out this answer from Brain Goetz on StackOverflow.

And be careful with what dependencies you add to your project as the problem that is part of that dependency becomes your problem too.

Spring Boot and Java Records

From version 2.5.0-M1 onwards, Spring Boot provides preliminary support for Java 16. I have a working Spring Boot application that I will use to demo Java records with it. The source code is available here. This is a simple Spring Boot application which, when accessed via /states or /state?name=statename REST endpoint show all or specific Indian states and their capitals. This application uses an in-memory H2 database that inserts rows at the start of the application.

As always, you can use start.spring.io to generate stubs for your Spring Boot application, and as explained earlier, make sure that you select the 2.5.x milestone version.

Here is how the REST controller class looks like.

https://medium.com/media/86ee01967c8b4df064b309a3ca4ddd15/href

We can focus on the getAllStates() method, which returns a list of State record class objects. We have already seen the State record class. There is no change in that.

public record State(String name, String capital) {}

Following is the StateRepository interface implemented by the StateService class.

https://medium.com/media/188296103c77cebc9ac9818342a1eb03/hrefhttps://medium.com/media/006c3e54c95e51068171f458ee8c9464/href

StateService is autowired using the constructor of the Controller class. It has a method named findAll() that uses Spring JdbcTemplate to query and returns a State record class list from the in-memory H2 database. As you can see, we have used the RowMapper functional interface, which JdbcTemplate uses for mapping rows of a ResultSet on a per-row basis, and it returns the Row object for the current row. We have also used the new keyword to initialize the record class, which means we can initialize the record class like normal Java classes. I have also used the Java 15 Text Blocks feature, which helps in the readability of SQL queries JSON string values.

However, there were some issues when I started using records with this application. Earlier I was using BeanPropertyRowMapper, which resulted in the following exception when I disabled Lombok and used Records instead for the State class.

2021-03-19 02:01:55.434 ERROR 66059 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.indianstates.State]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.indianstates.State.<init>()] with root cause

From the exception and BeanPropertyRowMapper documentation, it is pretty clear that we must declare a default or no-arg constructor in the State records class, which leads to some interesting discoveries about the records classes in Java. To solve this error, I naively added a no-arg constructor to the State record class.

public record State(String name, String capital) { 
public State() {}
}

But, that resulted in the following compilation error.

Non-canonical record constructor must delegate to another constructor

To solve this compilation, I added the following constructor, but this will make the response’s values.

public record State(String name, String capital) {
public State() {
this(null,null);
}
}

Then I took the help of the IntelliJ feature to generate the constructor for this record class. It provided me with the following options.

Genereate Record Constructor

I tried these options but got the same result. I already knew that these options wouldn’t work, but I tried my luck, which makes me wonder how to use records with BeanPropertyRowMapper. I don’t have an answer for this right now, but I will dig this further. If you see any issue with the code or have a better answer, then let me know.

Update:- I posted these exceptions to Spring Boot Gitter chat and got this answer.

BeanPropertyRowMapper can’t be used with records since it consists of creating an instance of a Java Bean with its no-arg constructor and then calling its setters to populate the bean. But records don’t have a no-arg constructor and are immutable and thus don’t have setters. So, either use a traditional Java Bean or use records, but then don’t use BeanPropertyRowMapper.

Fair enough. So clearly BeanPropertyRowMapper cannot be used with records. It’s a wrap for now. Happy coding.

Conclusion

In this article, you have learned that Records are immutable data carrier classes that reduce lots of boilerplate code that we are used to writing. Then we looked at the internals of a Record class and discovered that hashCode(), equals(), toString(), and constructors are implicitly generated for us by the compiler. Then we learned that you should not really compare or replace Records with external libraries like Lombok because both are different tools for different things.

In the last section, we discovered that Records are good for use in cases such as getting data from a database (or some external service) with an example of the Spring Boot application. We also discovered some issues while using BeanPropertyRowMapper, and concluded that we could not use it with records.

Please support me

If you like what you just read, then you can buy me a coffee by clicking the link in the image below:


Spring Boot and Java 16 Records was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Ashish Choudhary

In this article, we will discuss Java 16’s newest feature, Records. Then we will apply this knowledge and use it in conjunction with a Spring Boot application.

Photo by Jan Kopřiva on Unsplash

Introduction

On March 16th, 2021, Java 16 was GA. With this new release, tons of new exciting features are added. Check out the release notes to know more about these changes in detail. This article’s focus will be on Java Records, which got delivered with JEP 395. Records were first introduced in JDK 14 as a preview feature proposed by JEP 359, and with JDK 15, they remained in preview with JEP 384. However, with JDK 16, Records are no longer in preview.

I have picked records because they are definitely the most favored feature added in Java 16, according to this Twitter poll by Java Champion Mala Gupta.

I also conducted a similar survey, but it was focused on features from Java 8 onwards. The results were not unexpected as Java 8 is still widely used. Very unfortunate, though, as tons of new features and improvements are added to newer Java versions. But in terms of features, Java 8 was definitely a game-changer from a developer perspective.

For some more Java and tech-related stuff, follow me on Twitter

So let’s discuss what’s the fuss is about Java Records.

What are Records?

As per the JEP 395:

Records are a new kind of class in the Java language. They act as transparent carriers for immutable data with less ceremony than normal classes. Records can be thought of as nominal tuples.

Another quote from the JEP clearly explains developers’ frustration while writing typical data carrier classes.

Properly writing such a data-carrier class involves a lot of low-value, repetitive, error-prone code: constructors, accessors, equals, hashCode, toString, etc. For example, a class to carry x and y coordinates inevitably ends up like this:

Another option that we developers use most often is to leave the handling of the boilerplate to IDE. For example, with Intellij, you can generate constructors, getters, setters, equals, hashCode, and toString, etc., by simply pressing Command + N shortcut key. But the boilerplate code is still there.

Auto-generate code with IntelliJ

With Java 16 Records, it’s just one line of code. Cool, isn’t it.

record Point(int x, int y) { }

Here a record class declaration consists of a name, optional type parameters, a header, and a body.

Demystifying Records

Internals of the Java record class can be checked using a decompiler that comes with IntelliJ IDE or can use the javap command-line utility. To understand the internals, we created the following record class.

public record State(String name, String capital) {}

Following is the decompiled Java record class. I have used the javap command-line utility to check class file internals.

$ javap State.class

Following is the output.

Compiled from "State.java"
public final class com.example.indianstates.State extends java.lang.Record {
public com.example.indianstates.State(java.lang.String, java.lang.String);
public final java.lang.String toString();
public final int hashCode();
public final boolean equals(java.lang.Object);
public java.lang.String name();
public java.lang.String capital();
}

You can conclude the following from the above output.

  1. State record class is extending the java.lang.Record abstract class.
  2. State record class is declared final cannot be extended further using extends keyword.
  3. hashCode(), equals(), toString() and a canonical constructor are implicitly generated for us.
  4. There are no setters or getters, only accessors.
  5. With no setters and final in the class declaration clarifies that you cannot change the state, and hence records are immutable.

You can further validate these points by writing tests as well.

There are some restrictions in the declaration of record classes as compared to normal classes. Checkout JEP 395 for such restrictions.

Lombok and Records, Friends or Foe?.

You are probably already using Lombok annotations such as @ Value, which is closest if not the same to the Java records. Then you can get rid of one dependency and those Christmas trees of annotations. I might have oversimplified things, and it may make sense to replace Lombok for some cases. But you might be using Lombok for other features and not just one annotation that it provides. And believe me, while Java Records are a welcome feature for Java lovers, but it’s not going to replace Lombok, at least for now. You do not believe me? Check out this answer from Brain Goetz on StackOverflow.

And be careful with what dependencies you add to your project as the problem that is part of that dependency becomes your problem too.

Spring Boot and Java Records

From version 2.5.0-M1 onwards, Spring Boot provides preliminary support for Java 16. I have a working Spring Boot application that I will use to demo Java records with it. The source code is available here. This is a simple Spring Boot application which, when accessed via /states or /state?name=statename REST endpoint show all or specific Indian states and their capitals. This application uses an in-memory H2 database that inserts rows at the start of the application.

As always, you can use start.spring.io to generate stubs for your Spring Boot application, and as explained earlier, make sure that you select the 2.5.x milestone version.

Here is how the REST controller class looks like.

We can focus on the getAllStates() method, which returns a list of State record class objects. We have already seen the State record class. There is no change in that.

public record State(String name, String capital) {}

Following is the StateRepository interface implemented by the StateService class.

StateService is autowired using the constructor of the Controller class. It has a method named findAll() that uses Spring JdbcTemplate to query and returns a State record class list from the in-memory H2 database. As you can see, we have used the RowMapper functional interface, which JdbcTemplate uses for mapping rows of a ResultSet on a per-row basis, and it returns the Row object for the current row. We have also used the new keyword to initialize the record class, which means we can initialize the record class like normal Java classes. I have also used the Java 15 Text Blocks feature, which helps in the readability of SQL queries JSON string values.

However, there were some issues when I started using records with this application. Earlier I was using BeanPropertyRowMapper, which resulted in the following exception when I disabled Lombok and used Records instead for the State class.

2021-03-19 02:01:55.434 ERROR 66059 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.example.indianstates.State]: No default constructor found; nested exception is java.lang.NoSuchMethodException: com.example.indianstates.State.<init>()] with root cause

From the exception and BeanPropertyRowMapper documentation, it is pretty clear that we must declare a default or no-arg constructor in the State records class, which leads to some interesting discoveries about the records classes in Java. To solve this error, I naively added a no-arg constructor to the State record class.

public record State(String name, String capital) { 
public State() {}
}

But, that resulted in the following compilation error.

Non-canonical record constructor must delegate to another constructor

To solve this compilation, I added the following constructor, but this will make the response's values.

public record State(String name, String capital) {
public State() {
this(null,null);
}
}

Then I took the help of the IntelliJ feature to generate the constructor for this record class. It provided me with the following options.

Genereate Record Constructor

I tried these options but got the same result. I already knew that these options wouldn’t work, but I tried my luck, which makes me wonder how to use records with BeanPropertyRowMapper. I don't have an answer for this right now, but I will dig this further. If you see any issue with the code or have a better answer, then let me know.

Update:- I posted these exceptions to Spring Boot Gitter chat and got this answer.

BeanPropertyRowMapper can’t be used with records since it consists of creating an instance of a Java Bean with its no-arg constructor and then calling its setters to populate the bean. But records don’t have a no-arg constructor and are immutable and thus don’t have setters. So, either use a traditional Java Bean or use records, but then don’t use BeanPropertyRowMapper.

Fair enough. So clearly BeanPropertyRowMapper cannot be used with records. It's a wrap for now. Happy coding.

Conclusion

In this article, you have learned that Records are immutable data carrier classes that reduce lots of boilerplate code that we are used to writing. Then we looked at the internals of a Record class and discovered that hashCode(), equals(), toString(), and constructors are implicitly generated for us by the compiler. Then we learned that you should not really compare or replace Records with external libraries like Lombok because both are different tools for different things.

In the last section, we discovered that Records are good for use in cases such as getting data from a database (or some external service) with an example of the Spring Boot application. We also discovered some issues while using BeanPropertyRowMapper, and concluded that we could not use it with records.

Please support me

If you like what you just read, then you can buy me a coffee by clicking the link in the image below:


Spring Boot and Java 16 Records was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Ashish Choudhary


Print Share Comment Cite Upload Translate Updates
APA

Ashish Choudhary | Sciencx (2021-03-21T16:39:06+00:00) Spring Boot and Java 16 Records. Retrieved from https://www.scien.cx/2021/03/21/spring-boot-and-java-16-records-2/

MLA
" » Spring Boot and Java 16 Records." Ashish Choudhary | Sciencx - Sunday March 21, 2021, https://www.scien.cx/2021/03/21/spring-boot-and-java-16-records-2/
HARVARD
Ashish Choudhary | Sciencx Sunday March 21, 2021 » Spring Boot and Java 16 Records., viewed ,<https://www.scien.cx/2021/03/21/spring-boot-and-java-16-records-2/>
VANCOUVER
Ashish Choudhary | Sciencx - » Spring Boot and Java 16 Records. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/03/21/spring-boot-and-java-16-records-2/
CHICAGO
" » Spring Boot and Java 16 Records." Ashish Choudhary | Sciencx - Accessed . https://www.scien.cx/2021/03/21/spring-boot-and-java-16-records-2/
IEEE
" » Spring Boot and Java 16 Records." Ashish Choudhary | Sciencx [Online]. Available: https://www.scien.cx/2021/03/21/spring-boot-and-java-16-records-2/. [Accessed: ]
rf:citation
» Spring Boot and Java 16 Records | Ashish Choudhary | Sciencx | https://www.scien.cx/2021/03/21/spring-boot-and-java-16-records-2/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.