1

I want to have all my enum object capital. how to make it case insensitive.

HTTP body to JSON node and then node to a class object

JsonNode asJson = request().body().asJson();
WhatsappIncomingMessageVO requestVO = JsonParserUtils.fromJson(asJson, WhatsappIncomingMessageVO.class);

the json has lower case string "location" which i want map with LOCATION of below enum the class contains one enum which is :

public enum WhatsAppMessageType {

    TEMPLATE("TEMPLATE"),
    TEXT("TEXT"),
    IMAGE("image"), 
    VIDEO("video"), 
    AUDIO("audio"),
    @JsonProperty("json")
    LOCATION("location");

2 Answers2

8

If you are using Jackson... and I think yes, there are multiple solutions

1. Use an annotation with your @JsonProperty:

@JsonAlias({"location", "LOCATION", "Location"}) 

2. Write custom JsonDeserializer:

With the JsonDeserializer you can implement any logic you want.

3. Use the @JsonFormat annotation for your field:

@JsonFormat(with = JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) 

4. Set the format programmatically for all fields:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
Onema
  • 6,893
  • 11
  • 65
  • 99
kattoha
  • 141
  • 4
0
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);

This is what you need if you want enum values to be case insensitive.