[英]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
对象中的startDate
和dayDate
是正确的,但是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.