When it comes to using Jackson with Spring Boot, it's often the case that the default serialized property names will work just fine.

Usually, that means your JSON property names will match your java property names. They'll use camel case like:

lastName

firstName

But sometimes, that's not what you're looking for. You might prefer snake case or kebab case instead of camel case.

Heck, you might prefer all lower case.

Fortunately, you can add a consistent naming strategy to all your properties with one simple annotation at the top of the class.

And in this guide I'll show you how to do it.

An Example

Let's take a look at an example.

Suppose you have this simple Employee class that you're using for an HR app:

public class Employee {

    private String id;    
    private String lastName;
    private String firstName;

//getters and setters

}

And then you decide to serialize it with 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));

You'll get output that looks like this:

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

And that's fine and dandy if you want camel case. But suppose you want snake case?

Add @JsonNaming

In that case, you'd add the @JsonNaming annotation to the top of the class like this:

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Employee {

    private String id;    
    private String lastName;
    private String firstName;

...

}

Pay close attention to the fact that @JsonNaming doesn't operate on its own. You need to specify a naming strategy in parentheses right next to the annotation.

In the example above, the code specifies snake case. That should be pretty obvious from the name of the class.

So now if you serialize the Employee object with the same code as above, you'll get:

{
  "id" : "A17",
  "last_name" : "Smith",
  "first_name" : "Frank"
}

And that is correct because that is, in fact, snake case.

Other Options

But maybe you don't want either snake case or camel case. Fortunately, you've got options.

If you want kebab case, for example:

@JsonNaming(PropertyNamingStrategy.KebabCaseStrategy.class)
public class Employee {

    private String id;    
    private String lastName;
    private String firstName;

...

}

That will give you this output:

{
  "id" : "A17",
  "last-name" : "Smith",
  "first-name" : "Frank"
}

If you want everything in lower case, you can use:

@JsonNaming(PropertyNamingStrategy.LowerCaseStrategy.class)

You can also use lower case with dots, if you're of a mind:

@JsonNaming(PropertyNamingStrategy.LowerDotCaseStrategy.class)

Or you can go with camel case that capitalizes the first letter of every word (including the first word):

@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)

Feel free to go with the naming strategy that suits your business requirements.

Wrapping It Up

Now you know how to apply a naming strategy to an entire class. 

It's up to you to take what you've learned here and use it in the applications you're developing.

Have fun!

Photo by Donald Tong from Pexels