简体   繁体   English

如何设置处理程序以将具有多种命名 styles 的 JSON 字段绑定到 POJO

[英]How to set a handler for binding JSON fields which has a variety of naming styles to a POJO

I'm using Springboot 2.5.1, Jackson 2.13.1我正在使用 Springboot 2.5.1、Jackson 2.13.1

Input JSON looks like:输入 JSON 看起来像:

{
  "hello_word": "you are welcome",
  "my-name": "Meow",
  "Age": 11
}

Java POJO: Java POJO:

@Data
class A {

  private String helloWorld;

  private String myName;

  private Integer age;
}

Expected binding result:预期的绑定结果:

class A {

  private String helloWorld = "you are welcome";

  private String myName = "Meow";

  private Integer age = 11;
}

As you can see, there are 3 naming styles in the JSON.可以看到,JSON中有3个命名为styles。 I want to ask is there any way to set a handler or a subclass that can bind values correctly to POJO's fields in this case.我想问在这种情况下,有什么方法可以设置一个处理程序或一个子类,可以将值正确绑定到 POJO 的字段。

You can use @JsonProperty , for more examples您可以使用@JsonProperty ,获取更多示例

@Data
class A {

   @JsonProperty("hello_word")
   private String helloWorld;

   @JsonProperty("my-name")
   private String myName;

   @JsonProperty("Age")
   private Integer age;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM