V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
zeyexe
V2EX  ›  编程

[Spring Boot] 字段 eMail 被 Jackson 序列化为全小写的 email 的原因以及解决办法

  •  
  •   zeyexe · 2022-12-11 19:01:51 +08:00 · 763 次点击
    这是一个创建于 500 天前的主题,其中的信息可能已经有所发展或是发生改变。

    直接举例说明。Spring Boot 版本是 v2.7.5 ,Jackson 默认版本,Jackson 的property-naming-strategy是默认的LOWER_CAMEL_CASE,使用了 Lombok 。

    我现在有个 DTO:

    @Getter
    @Setter
    public class HelloReq {
    
        private String eMail;
        private String bColor;
    
    }
    

    有个 RestController 方法:

        @PostMapping(value = "/public/testWord")
        public HelloReq testDatetimeObj(@RequestBody HelloReq req) {
            return req;
        }
    

    当 Post 一个 JSON 过去:

    {
        "eMail": "[email protected]",
        "email": "[email protected]",
        "bcolor": "blue",
        "bColor": "black"
    }
    

    响应的是:

    {
        "email": "[email protected]",
        "bcolor": "blue"
    }
    

    也就是说 DTO 中aBcd形式的字段在序列化和反序列化时被当做abcd处理了。

    各位知道是什么原因吗?有什么解决办法。

    目前是通过@JsonProperty("bColor")注解解决这个问题,请问还有什么全局的办法吗?

    4 条回复    2022-12-12 17:34:04 +08:00
    asd999cxcx
        2
    asd999cxcx  
       2022-12-12 17:19:38 +08:00   ❤️ 1
    这个问题之前遇到过,好像是 JavaBean 规范和 Spring 的规范不一样导致的吧
    asd999cxcx
        3
    asd999cxcx  
       2022-12-12 17:21:10 +08:00   ❤️ 1
    具体可以参考这篇文章 https://www.jianshu.com/p/bef7b73e5062
    zeyexe
        4
    zeyexe  
    OP
       2022-12-12 17:34:04 +08:00
    @asd999cxcx 感谢,这个问题解释得很清楚了。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5312 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 08:07 · PVG 16:07 · LAX 01:07 · JFK 04:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.