简体   繁体   English

如何使用Jackson将蛇案Yaml映射到camelcase Java字段

[英]How to map snake case yaml to camelcase java fields with Jackson

I have a yaml file with keys in camel case, like 我有一个驼峰大小写的yaml文件,例如

---
start_date: "2018-09-01"
day_date: "2018-09-01"

userProduct:
    sales_channel: "1"
    user_group: "1" 

And I have the following Java PoJos 我有以下Java PoJos

public class Input{
    private String startDate = "";
    private String dayDate = "";

    @JsonUnwrapped
    private Product userProduct;

   // getters/setters
}

public class Product {
   private salesChannel = "";
   private userGroup = "";

   // getters/setters
}

Now I want to read this with Jackson and I thought it would be sufficient to use 现在我想和杰克逊一起阅读,我认为使用它就足够了

ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return objectMapper.readValue(inputData, Input.class); // inputData is the yaml as string

but this does not fill the nested objects. 但这不会填充嵌套对象。 The startDate and dayDate from the Input object is correct, but input.getUserProduct().getSalesChannel() for example returns an empty string. Input对象中的startDatedayDate是正确的,但是input.getUserProduct().getSalesChannel()返回一个空字符串。

I also tried to use user_product instead of userProduct in the yaml file, but this does not change anything. 我还尝试在yaml文件中使用user_product而不是userProduct ,但这不会改变任何内容。

What is wrong here ? 这是怎么了 How can I define snake case in the yaml and camelcase in java ? 如何在yaml和camelcase中用Java定义蛇形盒?

I have found a solution to my problem, which is to remove the nested objects. 我找到了解决我的问题的方法,那就是删除嵌套对象。 When having all fields inside the Input class and remove the Product , it works as expected. Input类中包含所有字段并删除Product ,它会按预期工作。

I keep this question open nevertheless, as I'm still interested, whether this can be achieved with nested objects 我仍然对此问题保持开放,因为我仍然很感兴趣,是否可以使用嵌套对象来实现

暂无
暂无

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

相关问题 Webclient + Jackson:如何设置反序列化将snake_case转换为camelCase? - Webclient + Jackson: how to setup deserialization to convert snake_case into camelCase? 如何 map Hibernate 实体字段使用camelCase到snake_case(下划线)数据库标识符 - How to map Hibernate entity fields using camelCase to snake_case (underscore) database identifiers 如何将firebase数据(snake_case)转换为Java对象(camelCase) - How to convert firebase data (snake_case) to Java object (camelCase) Java中从snake_case到camelCase - From snake_case to camelCase in Java SpringBoot:如何将蛇案例转换为骆驼案例 - SpringBoot: How to convert snake case to camelCase 如何在 Java 中使用蛇 yaml 序列化具有自定义名称的字段 - How to serialize fields with custom names using snake yaml in Java JAVA - 不使用 ObjectMapping 和 SetPropertyNamingStrategy 将 CamelCase 转换为 Snake_Case - SNAKE_CASE - JAVA - Not converting CamelCase to Snake_Case using ObjectMapping and SetPropertyNamingStrategy - SNAKE_CASE 如何将 JSON 密钥从驼峰式大小写更改为 POJO 的蛇式大小写? - How to change JSON key from camelCase to snake case for POJO? 将嵌套的 map 字段转换为蛇案例 - Convert nested map fields to snake case 除非明确指定,否则如何在默认情况下将 Spring Data JPA 从 camelCase 命名为 snake_case? - How to make Spring Data JPA by default naming from camelCase to snake_case unless explicitly specified?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM