Search code examples
javaspringspring-bootresttemplate

Spring restTemplate .postForObject() mapping cannot access element


Im trying to use the restTemplate.postForObject(URL, Session.class) method and map the response to a POJO. This works partially, however when i try to access an element with a name like "name-with-dashes" I cannot find the element.

The JSON I am extracting from the method call:

{"age":60,"expire":12345,"name-with-dashes":"This name has dashes?!"...}

Here is the POJO im using to extract this data:

@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Session {
        private int age;
        private long expire;
        //will not grab name-with-dashes... returns null
        private String nameWithDashes;
}

Solution

  • You should annotate your fields, especially the ones that do not comply to bean naming conventions, with the @JsonProperty annotation as follows:

    @JsonProperty("name-with-dashes")
    private String nameWithDashes;