[英]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_CASE
的PropertyNamingStrategy来控制字段名称的行为。 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.