Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map JSON default tag to java variable

I am consuming a rest service where the incoming JSon Response is something like this

 "thumbnailUrls": {
      "small": "skinresources/unpackaged/images/default_event.jpg",
      "medium": "skinresources/unpackaged/images/default_event.jpg",
      "large": "skinresources/unpackaged/images/default_event.jpg",
      "default": "skinresources/unpackaged/images/default_event.jpg"
    },

I have created a Java Class to map the Values Listed below is the Java Code Below

public class ThumbNailUrlDTO {

    private String small;

    private String medium;


    private String large;

    private String default;
}

The issue that I am having is I can't use the default name here as it is java keyword how do I deal with this problem

like image 915
developer2015 Avatar asked Sep 20 '26 08:09

developer2015


2 Answers

yes "default", invalid VariableDeclaratorId, you can not use default as variable name in java , its a pre-define keyword in java.

change variable name & map like this for json field name:- Use @JsonProperty :

public class ThumbNailUrlDTO {
    @JsonProperty("small")
    private String small;

    @JsonProperty("medium")
    private String medium;

    @JsonProperty("large")
    private String large;

    @JsonProperty("default")
    private String defaultStr;
}

This will solve the issue.

like image 179
Rohit Gupta Avatar answered Sep 21 '26 22:09

Rohit Gupta


you can use annotation to custom JSON name used for a property like this:

public class ThumbNailUrlDTO {

    private String small;

    private String medium;


    private String large;

    @JsonProperty("default")
    private String defaultVal;
}

In this way, you can avoid using java keyword "default".

like image 24
Felix Avatar answered Sep 21 '26 22:09

Felix