将一个类的命名策略设置为小驼峰命名法。

huangapple go评论114阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年7月30日 08:29:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63164408.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定