javaspringjacksonnaming-conventionscamelcasing

camelCase naming convention not working while returning response


Is camelCase naming convention failing in returning response, when variable name follows immediate camel casing rule?

example variable names such as, tId, iPhone, bLaBlAbLa, testName.

Steps to replicate issue

  1. create a spring-boot-starter-web
  2. create files as following:

TestModel.java

package com.test.IssueWithName.model;

import lombok.AllArgsConstructor;
import lombok.Getter;

@AllArgsConstructor
@Getter
public class TestModel {

    private int tId;
    private String iPhone;
    private int bLaBlAbLa;
    private String testName;

}

TestController.java

package com.test.IssueWithName.controller;

import com.test.IssueWithName.model.TestModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("test")
    public TestModel responseNamingIssue() {
        return new TestModel(1, "15 ultra super duper pro max", 4, "testingNameIssues bla bla");
    }

}

IssueWithNameApplication.java (main class)

package com.test.IssueWithName;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class IssueWithNameApplication {

    public static void main(String[] args) {
        SpringApplication.run(IssueWithNameApplication.class, args);
    }

}

response while hitting localhost:<port>/test

{
    "testName": "testingNameIssues bla bla",
    "tid": 1,
    "iphone": "15 ultra super duper pro max",
    "blaBlAbLa": 4
}

Problem: testName followed camelCasing rule properly but i guess tId, iPhone and bLaBlAbLa named variables failed camelCasing in response. tId, iPhone and bLaBlAbLa became tid, iphone and blaBlAbLa respectively. Please correct me if I'm wrong at somewhere. Thanks for reading.


Solution

  • Spring Boot uses Jackson as the default serialization/deserialization library.

    For Jackson to serialize a POJO, it looks for default getters (methods with prefix is for boolean, get for the other types) of fields.

    The problem you are seeing is due to the fact that Jackson uses Java Bean naming conventions to figure out the the JSON properties in a Java class.

    A similar issue has been raised before as well. Please check below mentioned link for more details related to this issue. Why does Jackson 2 not recognize the first capital letter if the leading camel case word is only a single letter long? Coming back to your question you can solve this issue by using @JsonProperty on your fields instead of using Lombok @Getter annotation for generating getter methods. Its a Jackson library annotation you can use to customize your JSON property name. Now your model class will look like this :

    @AllArgsConstructor

    public class TestModel {

    @JsonProperty
    private int tId;
    
    @JsonProperty
    private String iPhone;
    @JsonProperty
    private int bLaBlA;
    @JsonProperty
    private String testName;
    

    }

    After making these changes you will get response like this :

    { "tId": 1, "iPhone": "I Phone", "bLaBlA": 8, "testName": "Testing" }

    I have tested the above code in my local and its working fine. Hope this helps Happy Coding!