0
votes

In my dropwizard project I have bean classes, which are used in the Resource class like this :

@GET
@Path("/{id}")
public Response getUser(@PathParam("id") Long id) {
    return Response.ok(userDAO.get(id)).build();
}

class User {

    private String id;

    @JsonProperty("first_name")
    private String firstName;

    @JsonProperty("last_name")
    private String lastName;
}

Is there a way that I can add to dropwizard configuration or in the application class to tell javax to map my bean entity (user) to json using snake_case naming strategy. This will help me avoid using the @JsonProperty("first_name") annotation for every member of the class.

In the absence of the aforementioned annotation, my json looks like this :

{
    "firstName": "John",
    "lastName": "Doe"
}

I would rather like it to be :
{
    "first_name": "John",
    "last_name": "Doe"
}
2

2 Answers

1
votes

Found the answer on another SO question : How to make @JsonSnakeCase the default for configuration in Dropwizard

Adding the following line to application does the job !

environment.getObjectMapperFactory()
  .setPropertyNamingStrategy(
    PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES)
1
votes

You can annotate the whole entity with @JsonSnakeCase to avoid annotating each and every property.

Otherwise you can configure the global ObjectMapper in your application initialization

objectMapper.setPropertyNamingStrategy(
  PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES);