Wanna tighten your Spring Boot code up a bit?

Well, if you're using Jackson to serialize Java objects, I have a suggestion.

Get rid of the getters and setters in the classes that you serialize.

Yeah, I know. That's not an option all the time. But it might be an option if you're just serializing the objects and not doing anything with them on the Spring Boot side.

If you're curious, read on.

Take a Look at @JsonAutoDetect

Jackson offers an annotation called @JsonAutoDetect. You can use it to serialize private fields even if they don't have public getters and setters.

Let's take a look at it in action.

First, start by creating an Employee class.

public class Employee {

    private String id;    
    private String lastName;
    private String firstName;    
    private String shirtSize = "XXL";
    
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
    
    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }
}

Look carefully at that code up above and you'll see that shirtSize doesn't have a getter or setter. It's defaulted to "XXL."

Now let's serialize an instance of that class using this code:

ObjectMapper mapper = new ObjectMapper();            

Employee employee = new Employee();
employee.setId("A17");
employee.setFirstName("Frank");
employee.setLastName("Smith");

System.err.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee));

And you should see this result:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank"
}

No shirt size.

Let's fix that.

Fixing That

Now annotate the Employee class as follows:

@JsonAutoDetect(fieldVisibility = Visibility.ANY)
public class Employee {
...

The @JsonAutoDetect annotation doesn't operate alone. You need to tell it how to "detect."

That happens inside the parentheses next to @JsonAutoDetect.

In this case, the code sets fieldVisibility to Visibility.ANY.

As you've probably guessed, Visibility is an enum in the Jackson library. And Visibility.ANY here means that fields with any access modifiers get serialized.

Yes, even private ones.

Jackson can find the private fields thanks to the magic of reflection.

So now run that serialization code again and you should see the following output:

{
  "id" : "A17",
  "lastName" : "Smith",
  "firstName" : "Frank",
  "shirtSize" : "XXL"
}

Bingo! Now the "shirtSize" property is there and it's populated correctly.

Reminder, though: go this route with care.

If you really want to tighten your code by eliminating getters and setters, I'd recommend using a third-party library like Lombok instead.

Wrapping It Up

Okay. Now you know how to use @JsonAutoDetect to find private fields with no getters and setters during the Jackson serialization process.

All that's left for you now is to include what you've learned here in your own Spring Boot application development efforts.

Have fun!

Photo by Tim Samuel from Pexels