Provides comprehensive Jackson JSON processing support for Dropwizard applications with pre-configured ObjectMapper instances and essential modules.
—
Dropwizard Jackson provides flexible property naming support that allows classes to use either camelCase or snake_case JSON field names based on annotations.
A property naming strategy that conditionally applies snake_case naming based on the presence of the @JsonSnakeCase annotation.
public class AnnotationSensitivePropertyNamingStrategy extends PropertyNamingStrategy {
public String nameForConstructorParameter(MapperConfig<?> config,
AnnotatedParameter ctorParam,
String defaultName)
public String nameForField(MapperConfig<?> config,
AnnotatedField field,
String defaultName)
public String nameForGetterMethod(MapperConfig<?> config,
AnnotatedMethod method,
String defaultName)
public String nameForSetterMethod(MapperConfig<?> config,
AnnotatedMethod method,
String defaultName)
}Behavior:
@JsonSnakeCaseMarker annotation that indicates a class should use snake_case property names in JSON serialization.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonSnakeCase {
}Properties:
@JsonSnakeCase
public class UserPreferences {
private String displayName;
private boolean enableNotifications;
private int maxRetryCount;
// constructors, getters, setters...
}
ObjectMapper mapper = Jackson.newObjectMapper();
UserPreferences prefs = new UserPreferences("John Doe", true, 3);
String json = mapper.writeValueAsString(prefs);
// Result: {"display_name":"John Doe","enable_notifications":true,"max_retry_count":3}// This class uses default camelCase
public class StandardConfig {
private String serverName;
private int portNumber;
// constructors, getters, setters...
}
// This class uses snake_case
@JsonSnakeCase
public class DatabaseConfig {
private String connectionUrl;
private int maxConnections;
// constructors, getters, setters...
}
ObjectMapper mapper = Jackson.newObjectMapper();
StandardConfig standard = new StandardConfig("api-server", 8080);
DatabaseConfig database = new DatabaseConfig("jdbc:postgresql://localhost/db", 10);
String standardJson = mapper.writeValueAsString(standard);
// Result: {"serverName":"api-server","portNumber":8080}
String databaseJson = mapper.writeValueAsString(database);
// Result: {"connection_url":"jdbc:postgresql://localhost/db","max_connections":10}@JsonSnakeCase
public class ApiKey {
private final String keyValue;
private final long createdAt;
public ApiKey(@JsonProperty("key_value") String keyValue,
@JsonProperty("created_at") long createdAt) {
this.keyValue = keyValue;
this.createdAt = createdAt;
}
// getters...
}
ObjectMapper mapper = Jackson.newObjectMapper();
// Both serialization and deserialization use snake_case
String json = "{\"key_value\":\"abc123\",\"created_at\":1640995200000}";
ApiKey key = mapper.readValue(json, ApiKey.class);
String serialized = mapper.writeValueAsString(key);
// Result: {"key_value":"abc123","created_at":1640995200000}The naming strategy is automatically configured when using Jackson factory methods:
ObjectMapper mapper = Jackson.newObjectMapper();
// AnnotationSensitivePropertyNamingStrategy is automatically set
ObjectMapper minimal = Jackson.newMinimalObjectMapper();
// Does NOT include the custom naming strategy (uses default)The naming strategy works with:
Note: The strategy only affects classes directly annotated with @JsonSnakeCase. Nested objects follow their own annotation rules.
Install with Tessl CLI
npx tessl i tessl/maven-io-dropwizard--dropwizard-jackson