I am using Quarkus and Microprofile OpenAPI to map entities in REST APIs. 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.
The standard best practice for REST APIs is to have a hyphen, not camelcase or underscores. This comes from Mark Masse's "REST API Design Rulebook" from Oreilly.
Camel case (sometimes stylized as camelCase or CamelCase, also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation. It indicates the separation of words with a single capitalized letter, and the first word starting with either case.
Use camelCase for Parameters If you look at the https://api.example.com/users/1234 link, the 1234 is the userId here. With the above, the user is able to query a user based on their user ID. Now, for the naming convention, it is advisable to use camelCase instead of others.
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. In Quarkus, you can use either Jackson or JsonB for object mapping.
For Jackson, you can control the behaviour of field names using PropertyNamingStrategy which you want to set to SNAKE_CASE
. To set this globally, create an ObjectMapperCustomizer
like so:
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 e.g. ignore unknown props during deserialization, date formatting, etc.
You need to have a dep to 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
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With