Spring Boot接口返回JSON时特殊驼峰(camelCase)命名变量序列化异常问题咨询
Spring Boot接口返回JSON时特殊驼峰(camelCase)命名变量序列化异常问题咨询
你好,我最近碰到了一个Spring Boot接口返回JSON时的序列化问题,关于特殊规则的驼峰命名变量,想请教各位帮忙分析下原因。
我的疑问是:当变量名遵循“首字母小写+第二个字母大写”这类特殊驼峰规则时(比如tId、iPhone、bLaBlAbLa),返回的JSON响应里的字段名没有保持原有的驼峰格式,而像testName这种标准驼峰命名的变量却能正常返回,这是序列化规则的问题吗?
问题复现步骤
- 创建一个
spring-boot-starter-web项目 - 创建以下三个核心文件:
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(主启动类)
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); } }
实际返回结果
调用localhost:<port>/test接口后,得到的JSON响应如下:
{ "testName": "testingNameIssues bla bla", "tid": 1, "iphone": "15 ultra super duper pro max", "blaBlAbLa": 4 }
问题点
可以看到:
- 标准驼峰命名的
testName正常返回为testName - 特殊驼峰命名的
tId变成了tid,iPhone变成了iphone,bLaBlAbLa变成了blaBlAbLa
我想确认一下,是不是我对驼峰命名规则的理解有误?还是Spring Boot默认的JSON序列化(Jackson)有特定的转换规则?麻烦各位帮忙解答,谢谢!
备注:内容来源于stack exchange,提问作者Akash Prajapati
相关产品推荐
相关产品推荐

