Java JsonNaming-class And Method Code Example


import com.fasterxml.jackson.annotation.JsonNaming;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;

@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
public class Person {
    private String firstName;
    private String lastName;

    public Person(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }
}

This is an example of how to use the com.fasterxml.jackson.annotation.JsonNaming annotation in Jackson. The @JsonNaming annotation is used to specify a PropertyNamingStrategy that should be used to map the field names to JSON property names when serializing the class.

In this example, the Person class has two fields, firstName and lastName, and the @JsonNaming annotation is used to specify that the PropertyNamingStrategy.SnakeCaseStrategy should be used to map the field names to JSON property names.

When serializing an instance of this class to JSON, the resulting JSON will have properties named first_name and last_name instead of firstName and lastName.

It's important to note that JsonNaming annotation can be applied to class level, package level or even to a ObjectMapper. If you want to apply the strategy only to certain fields, you can use @JsonProperty annotation with value parameter.