英文:
Set naming strategy to lower camel case for one class
问题
我的项目已经定义了一个ObjectMapper,如下所示:
final ObjectMapper mapper = new ObjectMapper();
// 其他一些设置在这里
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
实际上,我的项目中大多数类都使用SNAKE_CASE
,一切都很顺利。现在我有一个类,下游以小驼峰命名返回。我尝试在我的类上使用@JsonNaming(PropertyNamingStrategy.LowerCamelCaseStrategy)
。如果存在这样的策略,那将会起作用,但似乎在PropertyNamingStrategy
内部没有类似的选项。
我发现与小驼峰命名有关的只有常量PropertyNamingStrategy.LOWER_CAMEL_CASE
,但似乎只在顶部的代码段中使用,我绝对不想在这里覆盖项目设置,因为几乎所有的类都使用蛇形命名法。
当然,我可以在类中的每个属性上添加@JsonProperty('propertyInLowerCamelCase')
,但属性有相当多,这似乎不应该是必需的。
英文:
My project already has an ObjectMapper defined, as follows
final ObjectMapper mapper = new ObjectMapper();
// some other settings here
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
Most classes in my project in fact use SNAKE_CASE
, and everything's peachy. Now I have a class that the downstream returns in lower camel case. I tried using @JsonNaming(PropertyNamingStrategy.LowerCamelCaseStrategy)
on my class. If it existed that'd work, but nothing like that seems to exist inside PropertyNamingStrategy
.
All I find referencing lower camel case is the constant PropertyNamingStrategy.LOWER_CAMEL_CASE
which is only used like in the top snippet it seems, and I definitely don't want to override the project settings here, since almost all classes use snake case.
Of course, I could slap a @JsonProperty('propertyInLowerCamelCase')
on every property in the class, but there's quite a few, and that seems like it shouldn't be necessary.
答案1
得分: 3
要强制使用 lowerCamelCase,您可以使用以下注解:
@JsonNaming(PropertyNamingStrategy::class)
这将强制使用默认命名策略(lowerCamelCase),并覆盖全局配置的命名策略。
英文:
To enforce lowerCamelCase, you can use the annotation
@JsonNaming(PropertyNamingStrategy::class)
This enforces the default naming strategy (lowerCamelCase) and overrides a globally configured naming strategy.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论