1. Overview

Jackson is a very popular library to convert Java object to JSON. while converting POJO to JSON, It will consider property name will as key. But sometimes we require to customize the properties name. Jackson provides a different naming strategy for key names like SnakeCaseStrategyPascalCaseStrategy, LowerCaseStrategy, KebabCaseStrategy.

In this article, we will discuss how we can change naming policy while converting POJO to JSON using Jackson. Here is a details explanation about those strategies.

EmployeeController.java

Here we have defined RestController in spring boot application so while returning POJO as a response then it will automatically JSON using Jackson with default naming strategy.

@RestController
public class EmployeeController {
    @RequestMapping("/getEmployee")
    public Employee getEmployees() {
        Employee employee=new Employee();
        employee.setEmployeeId(19);
        employee.setEmployeeName("Harry");
        employee.setEmployeeRole("DEVELOPER");
        return employee;
    }
}

Employee.java

Here we have defined Employee POJO object as per camel notation naming conversation.

public class Employee {
    private int employeeId;
    private String employeeName;
    private String employeeRole;
    public int getEmployeeId() { return employeeId; }
    public void setEmployeeId(int employeeId) { this.employeeId = employeeId;}
    public String getEmployeeRole() { return employeeRole; }
    public void setEmployeeRole(String employeeRole) { this.employeeRole = employeeRole; }
    public String getEmployeeName() { return employeeName; }
    public void setEmployeeName(String employeeName) { this.employeeName = employeeName; }
}

2. Spring Jackson property naming strategy

If we are using spring boot for application development then we can configure spring.jackson.property-naming-strategy with naming strategy inside application.properties or application.yml. or without spring boot application or spring MVC application, we can register bean of Jackson2ObjectMapperBuilder and define configuration related to naming strategy.

Here are five possible strategies which are supported by Jackson:

  1. LOWER_CAMEL_CASE
  2. LOWER_CASE
  3. SNAKE_CASE
  4. UPPER_CAMEL_CASE
  5. KEBAB_CASE

1. LowerCamelCase

  • It same as naming conversation used in JAVA where the first letter is small and remaining letter word’s first letter will be capital.

application.properties

spring.jackson.property-naming-strategy=LOWER_CAMEL_CASE

or

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
     Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder();
     b.propertyNamingStrategy(PropertyNamingStrategy.LOWER_CAMEL_CASE);
     return b;
}

Output:

http://localhost:8080/getEmployee

  {
    "employeeId": 19,
    "employeeName": "Harry",
    "employeeRole": "DEVELOPER"
  }

2. LowerCaseStrategy

It will convert key into the lower case, there is no any separator between those words.

application.properties

spring.jackson.property-naming-strategy=LOWER_CASE

or

If do not have application.properties file than create a bean with following configurations.

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
     Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder();
     b.propertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE);
     return b;
}

Output:

http://localhost:8080/getEmployee

  {
    "employeeid": 19,
    "employeename": "Harry",
    "employeerole": "DEVELOPER"
  }

2. SnakeCaseStrategy

In Snake Case Strategy, all words are in small with _ (underscore) as a separator.

application.properties

spring.jackson.property-naming-strategy=SNAKE_CASE

or

If we do not have application.properties file than create a bean with following configurations.

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
     Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder();
     b.propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
     return b;
}

Output:

http://localhost:8080/getEmployee

  {
    "employee_id": 19,
    "employee_name": "Harry",
    "employee_role": "DEVELOPER"
  }

3. UpperCamelCaseStrategy

In Upper camel case strategy, the First letter of all the words will be capital.

application.properties

spring.jackson.property-naming-strategy=UPPER_CAMEL_CASE

or

If we  do not have application.properties file than create a bean with following configurations.

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
     Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder();
     b.propertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
     return b;
}

Output:

http://localhost:8080/getEmployee

  {
    "EmployeeId": 19,
    "EmployeeName": "Harry",
    "EmployeeRole": "DEVELOPER"
  }

4. KebabCaseStrategy

In Snake Case Strategy, all words are in small with – (Hyphen) as a separator.

application.properties

spring.jackson.property-naming-strategy=KEBAB_CASE

or

If we do not have application.properties file than create a bean with following configurations.

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
     Jackson2ObjectMapperBuilder b = new Jackson2ObjectMapperBuilder();
     b.propertyNamingStrategy(PropertyNamingStrategy.KEBAB_CASE);
     return b;
}

Output:

http://localhost:8080/getEmployee

  {
    "employee-id": 19,
    "employee-name": "Harry",
    "employee-role": "DEVELOPER"
  }

3. Conclusion

In this article, We learn that how we can change the field name strategy while converting data to JSON response using SnakeCaseStrategyPascalCaseStrategy, LowerCaseStrategy, KebabCaseStrategy. We have discussed configuration using application.properties and Jackson2ObjectMapperBuilder bean, both of them requires anyone to be configured not both.

4. References

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *