![](/img/trans.png)
[英]Webclient + Jackson: how to setup deserialization to convert snake_case into camelCase?
[英]How to map snake case yaml to camelcase java fields with Jackson
我有一個駝峰大小寫的yaml文件,例如
---
start_date: "2018-09-01"
day_date: "2018-09-01"
userProduct:
sales_channel: "1"
user_group: "1"
我有以下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
}
現在我想和傑克遜一起閱讀,我認為使用它就足夠了
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return objectMapper.readValue(inputData, Input.class); // inputData is the yaml as string
但這不會填充嵌套對象。 Input
對象中的startDate
和dayDate
是正確的,但是input.getUserProduct().getSalesChannel()
返回一個空字符串。
我還嘗試在yaml文件中使用user_product
而不是userProduct
,但這不會改變任何內容。
這是怎么了 如何在yaml和camelcase中用Java定義蛇形盒?
我找到了解決我的問題的方法,那就是刪除嵌套對象。 在Input
類中包含所有字段並刪除Product
,它會按預期工作。
我仍然對此問題保持開放,因為我仍然很感興趣,是否可以使用嵌套對象來實現
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.