Search code examples
javajacksonavro

Is it possible to make POJOs generated with the Avro Maven plugin play nice with Jackson?


The problem I'm having with Avro is that we are required to use title case names for our data model, but camel case for the rest of our data structures such as DTOs. Here is an example:

{
  "name": "UserRecord",
  "type": "record",
  "fields": [
    {
      "name": "Username",
      "type": "string"
    }
  ]
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CreateUserRequestDTO {
  private UserRecord userRecord;
}

The Maven Avro plugin will generate the Username field like this:

private String Username;

So we have an ugly mix in our code of fields that are title-cased in some cases and camel cased in others. Is there a good way to make Jackson's ObjectMapper able to read both of these property name conventions?

The case mixing is resulting in the Avro fields that are title-cased not getting recognized by Spring Boot's ObjectMapper and all of their fields are null.


Solution

  • I do not know if it will actually work in your use case, but maybe you can enable the Jackson's mapper configuration property ACCEPT_CASE_INSENSITIVE_PROPERTIES:

    If enabled, the bean properties will be matched using their lower-case equivalents, meaning that any case-combination (incoming and matching names are canonicalized by lower-casing) should work.

    You can customize the value in your application configuration file under the configuration key spring.jackson.mapper.*, something like:

    spring:
      jackson:
          mapper:
              ACCEPT_CASE_INSENSITIVE_PROPERTIES: true