1

I have following JSON response coming from a Rest call:

{
  "config" : {
       "hour" : 1
       "minute" : 60
       "pw" : "password"
   },
  "id" : 12345,
  "enabled" : true,
  "name" : "my-name"
}

I am using Spring RestTemplate to make the rest call and I would like to map the response to a Java Object as below:

public Class MyResponse {
    private Map<String, String> config;

    private Map<String, String> allTheRestProps;
}

Is it possible to do this with Jackson annotations without using String as a response and map it manually?

1
  • So you want to put id, enabled, name in allTheRestProps? Commented Oct 8, 2019 at 15:23

1 Answer 1

1

Use JsonAnySetter annotation:

class MyResponse {

    private Map<String, String> config;
    private Map<String, String> allTheRestProps = new HashMap<>();

    public Map<String, String> getConfig() {
        return config;
    }

    public void setConfig(Map<String, String> config) {
        this.config = config;
    }

    public Map<String, String> getAllTheRestProps() {
        return allTheRestProps;
    }

    @JsonAnySetter
    public void setAllTheRestProps(String key, String value) {
        this.allTheRestProps.put(key, value);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.