Search code examples
javajsonspring-mvcjacksonjackson-databind

Case Insensitive for @JsonSubTypes


@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes({
        @Type(value = CertificateAuth.class, name = "certificate"),
        @Type(value = UsernamePasswordCredentials.class, name = "usernamepassword"),
        @Type(value = APIKeyCredentials.class, name = "apikey")
})

In request json, for type value how can I make that as case insensitive?

For eg: type should take "usernamepassword" (or) "userNamePassword" (or) USERNAMEPASSWORD.

Please Note: Json to POJO mapping happens automatically as like RestController

Kindly help me


Solution

  • If you need to convert the type value to a lowercased string you can convert the json file to a JsonNode tree and after convert it to your pojo with the ObjectMapper#treeToValue method:

    JsonNode root = mapper.readTree(json);
    ((ObjectNode) root).put("type", root.get("type").asText().toLowerCase());
    YourPojo yourPojo = mapper.treeToValue(root, YourPojo.class);