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

示例变量名称,例如tIdiPhonebLaBlAbLatestName.

Steps to replicate issue

  1. 创建一个Spring-Boot-starter-web
  2. 按如下方式创建文件:

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的同时做出react

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

问题:testName正确地遵循了camelCase规则,但我猜tIdiPhonebLaBlAbLa命名变量没有响应camelCase.tIdiPhonebLaBlAbLa分别变成了tidiphoneblaBlAbLa. 如果我说错了,请纠正我.谢谢你的阅读.

推荐答案

Spring Boot使用Jackson作为默认的序列化/非序列化库.

对于Jackson序列化POJO,它寻找字段的默认getter(带有前缀的方法用于boolean,get用于其他类型).

您看到的问题是由于Jackson使用Java Bean命名约定来确定Java类中的JSON属性.

类似的问题以前也曾被提出过.有关此问题的更多详细信息,请查看下面提到的链接. Why does Jackson 2 not recognize the first capital letter if the leading camel case word is only a single letter long?个 回到您的问题,您可以通过在您的字段上使用@JsonProperty来解决这个问题,而不是使用Lombok@getter注释来生成getter方法.这是Jackson库注释,您可以使用它来自定义您的JSON属性名称. 现在,您的模型类将如下所示:

@AllArgsConstructor

公共类TestModel{

@JsonProperty
private int tId;

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

}

进行这些更改后,您将得到如下响应:

{ "TID":1, "iphone":"iphone", 《blabla》:8, "TestName":"测试中" }

我已经在本地测试了上面的代码,它工作得很好.希望这能有所帮助 编码快乐!

Java相关问答推荐

编译期间错误(Java 0000)Android .Net MAUI

Maven Google Sheets版本问题

具有额外列的Hibert多对多关系在添加关系时返回NonUniqueHealthExcellent

屏蔽字母数字代码的Java正则表达式

缩小画布比例后更改滚动窗格的内部大小

为什么我的ArrayList索引的索引总是返回-1?

为什么不应用类型推断?

如何调整工作时间日历中的时间

为什么Java编译器不区分不同类型的方法?

Jenv-相同的Java版本,但带有前缀

支持MySQL 5.6的最新Hibernate版本

A.ForEach与For(类型a:集合)

如何在EL处理器中定义带有命名空间的变量?

如何在Record Java中使用isRecord()和RecordComponent[]?

如何在Java中使用正则表达式拆分字符串

Java.time.OffsetDateTime的SQL Server数据库列类型是什么?

如何调整JButton的大小以适应图标?

如何在ApacheHttpClient 5中为单个请求设置代理?

循环不起作用只有第一个元素重复

转换为JSON字符串时,日期按天递减-Java