Search code examples
javajsonjacksonresponsenetflix-feign

JSON response with dash in the key-name


I am using feign for my rest-calls. Unfortunately one of the responses I get looks something like this:

{
    "customer-id" : "0123"
}

The JSON response automatically gets mapped to one of my POJO's. This response object can not have a property field with the name "customer-id", as the dash (-) is not allowed in the name of an identifier.

I tried the following:

public class LookUpAccountsResponse {
        @JsonProperty("customer-id")
        private String customerId;
}

But unfortunately this doesn't work. Does anyone have a suggestion on how to fix this?


Solution

  • com.google.gson.GsonDecoder

    Not sure why JsonProperty is on your classpath, but see "field naming support" https://github.com/google/gson/blob/master/UserGuide.md#json-field-naming-support

    @SerializedName is the Gson annotation you'll want

    Or switch entirely to using the feign-jackson dependency with a JacksonDecoder