JSON response with dash in the key-name

Robert van der Spek picture Robert van der Spek · Apr 21, 2017 · Viewed 7.7k times · Source

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?

Answer

OneCricketeer picture OneCricketeer · Apr 21, 2017

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