Spring Boot Java framework: use ReflectionToStringBuilder to improve development productivity

Data Backend Tech
2 min readMay 9, 2023

--

Introduction

ReflectionToStringBuilder is a utility class in the Apache Commons Lang library that helps to create a string representation of an object’s state using reflection. It can be useful in debugging, logging, and other scenarios where we need to understand the current state of an object. In this article, we will explore how to use ReflectionToStringBuilder in a Spring Boot application to improve development productivity by excluding certain fields from the output string.

Step by step instruction

  1. Add the Apache Commons Lang dependency to your project’s pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>

2. Create a class that you want to generate a string representation of using ReflectionToStringBuilder. For example:

public class Person {
private String name;
private int age;
private String email;
// constructor, getters and setters
}

3. Use ReflectionToStringBuilder to generate the string representation of the Person object in your code. By default, all fields of the object will be included in the output string:

Person person = new Person("Fake Name", 30, "fake.name@example.com");
String personAsString = ReflectionToStringBuilder.toString(person);
System.out.println(personAsString);

Output:

Person@57a23b66[name=Fake Name,age=30,email=fake.name@example.com]

4. To exclude certain fields from the output string, you can use the exclude method of the ReflectionToStringBuilder class. For example, to exclude the email field:

String personAsString = new ReflectionToStringBuilder(person)
.exclude("email")
.toString();
System.out.println(personAsString);

Output:

Person@57a23b66[name=Fake Name,age=30]

5. You can also exclude multiple fields by passing them as a comma-separated string or an array:

String[] excludeFields = {"age", "email"};
String personAsString = new ReflectionToStringBuilder(person)
.exclude(excludeFields)
.toString();
System.out.println(personAsString);

Output:

Person@57a23b66[name=Fake Name]

6. You can also leverage ReflectionToStringBuilder to generate multiline string representations of objects.

public class Person {
private String name;
private int age;
private String email;

// Constructors, getters, setters, and other methods omitted for brevity

@Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
}
}

Output:

Person@1b6d3586[
name=Fake Name
age=30
address=a@b.com
]

Summary

Using ReflectionToStringBuilder can be a useful tool in debugging and logging in a Spring Boot application. By excluding certain fields from the output string, you can focus on the relevant information and improve development productivity. By following the steps outlined in this article, you can use ReflectionToStringBuilder to generate string representations of objects that exclude certain fields in your Spring Boot application.

--

--