简体   繁体   English

如何在 REST API 中将骆驼大小写转换为带下划线的小写字母?

[英]How to convert camel case to lower case with underscores in a REST API?

I am using Quarkus and Microprofile OpenAPI to map entities in REST APIs.我正在使用 Quarkus 和 Microprofile OpenAPI 到 REST API 中的 map 实体。 I can convert my camel case named properties to lower case with underscores in the following way:我可以通过以下方式将我的驼峰命名属性转换为带下划线的小写:

@Schema(name = "first_name")
private String firstName;

However it is inconvenient as I have to do this everywhere across the project.然而,这很不方便,因为我必须在整个项目的任何地方都这样做。

Question: Is there a way to do it automatically for all properties without having to specify the mapping in the annotation?问题:有没有一种方法可以自动为所有属性执行此操作,而无需在注释中指定映射?

I went through the documentation of Quarkus and Microprofile but haven't found how it can be achieved.我浏览了 Quarkus 和 Microprofile 的文档,但还没有找到它是如何实现的。

If you want to make this behaviour the default one, you have to configure this in the object mapper that is responsible for serialization/deserialization of objects to json.如果您想将此行为设为默认行为,则必须在 object 映射器中进行配置,该映射器负责将对象序列化/反序列化为 json。 In Quarkus, you can use either Jackson or JsonB for object mapping.在 Quarkus 中,您可以使用 Jackson 或 JsonB 进行 object 映射。

For Jackson, you can control the behaviour of field names using PropertyNamingStrategy which you want to set to SNAKE_CASE .对于 Jackson,您可以使用要设置为SNAKE_CASEPropertyNamingStrategy来控制字段名称的行为。 To set this globally, create an ObjectMapperCustomizer like so:要全局设置,请创建一个ObjectMapperCustomizer ,如下所示:

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import io.quarkus.jackson.ObjectMapperCustomizer;

import javax.inject.Singleton;

@Singleton
public class ObjectMapperConfig implements ObjectMapperCustomizer {

    @Override
    public void customize(ObjectMapper objectMapper) {
         objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
    }
}

You can control many more aspects of serialization eg ignore unknown props during deserialization, date formatting, etc.您可以控制序列化的更多方面,例如在反序列化、日期格式化等期间忽略未知道具。

You need to have a dep to quarkus-resteasy-jackson :您需要对quarkus-resteasy-jackson有一个部门:

<dependency>
   <groupId>io.quarkus</groupId>
   <artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>

If you want to use JsonB ( quarkus-resteasy-jsonb ) then you can try it with the following JsonbConfigCustomizer如果您想使用 JsonB ( quarkus-resteasy-jsonb ),那么您可以尝试使用以下JsonbConfigCustomizer

import io.quarkus.jsonb.JsonbConfigCustomizer;

import javax.inject.Singleton;
import javax.json.bind.JsonbConfig;
import javax.json.bind.config.PropertyNamingStrategy;
@Singleton
public class JsonBCustomizer implements JsonbConfigCustomizer {

    public void customize(JsonbConfig config) {
        config.withPropertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE_WITH_UNDERSCORES);
    }
}

I found this in the openapi doc:我在 openapi 文档中找到了这个:

You can control how the Schema property names are dumped by setting the micronaut.openapi.property.naming.strategy system property.您可以通过设置 micronaut.openapi.property.naming.strategy 系统属性来控制 Schema 属性名称的转储方式。 It accepts one of the following jackson's PropertyNamingStrategy: - SNAKE_CASE, - UPPER_CAMEL_CASE, - LOWER_CAMEL_CASE, - LOWER_CASE and - KEBAB_CASE.它接受以下杰克逊的 PropertyNamingStrategy 之一:- SNAKE_CASE、- UPPER_CAMEL_CASE、 - LOWER_CAMEL_CASE、 - LOWER_CASE 和 - KEBAB_CASE。

For further details see Hibernate 5 Naming Strategy Configuration by baeldung .有关详细信息,请参阅 baeldung 的Hibernate 5 命名策略配置

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

相关问题 在swagger.json中没有反映CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES - CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES not reflected in swagger.json 我应该如何将 JSON 有效载荷的大小写转换为小驼峰大小写? - How should I convert the case of a JSON payload to lower camel case? "当有连续的​​大写字母时,如何在Java中将骆驼大小写转换为低连字符" - How to convert Camel Case to Lower Hyphen in Java when there are contiguous capitals Jackson 克服下划线,支持驼峰式 - Jackson overcoming underscores in favor of camel-case 如何将小写字母转换为大写字母 &amp; 以及将大写字母转换为小写字母 - how to convert Lower case letters to upper case letters & and upper case letters to lower case letters 泽西JSON从骆驼箱切换到下划线(蛇盒) - Jersey JSON switching from camel case to underscores (snake case) 将hashmap转换为小写 - convert hashmap to lower case 如何将大写字符串转换为Camel Case并保持首字母缩略词不变? - How to Convert Upper case string to Camel Case and keeping the Acronyms as it is? 无法使用 Jackson 将下划线大小写转换为驼色大小写 - Not able to convert underscore case to camel case with Jackson 如何在phpstorms(基于速度)文件中将字符串转换为驼峰情况? - How to convert a string to camel case in phpstorms (velocity based) file tempates?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM