0

Getting both java obejct as will jsonproperty is getting generated whilc I convert java object to JSON.

Can you please confirm where I have made mistake.

bharathi
  • 5,311
  • 20
  • 79
  • 131
  • See https://stackoverflow.com/q/25893985/362792 – Hitobat Jun 12 '18 at 15:12
  • @Hitobat The problem is I need the xApiheader and xApiheaderCc. As if you see I have changed the name to x-apiheader-cc and x-apiheader in sample JSON. But I am getting all xApiheader and xApiheaderCc(which should be as x-apiheader-cc and x-apiheader) – bharathi Jun 12 '18 at 15:16
  • You can use @JsonIgnore for the fields which you don't want to serialize. – Sairam Kukadala Jun 12 '18 at 15:21
  • Try removing your getter, or move JsonProperty annotation to getter method. – Hitobat Jun 12 '18 at 15:22

2 Answers2

0

Add @JsonAutoDetect(getterVisibility= JsonAutoDetect.Visibility.NONE) to your class:

@JsonAutoDetect(getterVisibility= JsonAutoDetect.Visibility.NONE)
@JsonInclude(JsonInclude.Include.NON_EMPTY) 
public class FalconidePersonalizationVO {

By default Jackson follow java bean convention to output json properties. As result, it founds your getX method and output xapiheader property.

But you also annotate your field with @JsonProperty so another property named x-apiheader is also ouputed.

Disable getterX detection method will prevent jackson output getter fields.

Mạnh Quyết Nguyễn
  • 15,590
  • 1
  • 17
  • 41
0

**************** Solution 1 ****************

Annotate getter / setter with @JsonProperty as well (now annotating field is not mandatory)

public class FalconidePersonalizationVO {

    @JsonProperty("x-apiheader-cc")
    private String xApiheaderCc;

    @JsonProperty("x-apiheader")
    private String xApiheader;

    @JsonProperty("x-apiheader-cc")
    public String getXApiheaderCc() {
        return xApiheaderCc;
    }

    @JsonProperty("x-apiheader-cc")
    public void setXApiheaderCc(String xApiheaderCc) {
        this.xApiheaderCc = xApiheaderCc;
    }

    @JsonProperty("x-apiheader")
    public String getXApiheader() {
        return xApiheader;
    }

    @JsonProperty("x-apiheader")
    public void setXApiheader(String xApiheader) {
        this.xApiheader = xApiheader;
    }
}   

**************** Solution 2 ****************

Follow setter / getter naming convention. In normal naming convention first letter of field name is capitalized prepending it with set / get. But in this case since second char is capital so, first char is not capitalized. See https://stackoverflow.com/a/16146215/3295987

public class FalconidePersonalizationVO {

    @JsonProperty("x-apiheader-cc")
    private String xApiheaderCc;

    @JsonProperty("x-apiheader")
    private String xApiheader;

    /*
     * Setter / getter auto generated in eclipse
     */
    // getXApiheaderCc -> getxApiheaderCc
    public String getxApiheaderCc() {
        return xApiheaderCc;
    }

    public void setxApiheaderCc(String xApiheaderCc) {
        this.xApiheaderCc = xApiheaderCc;
    }

    public String getxApiheader() {
        return xApiheader;
    }

    public void setxApiheader(String xApiheader) {
        this.xApiheader = xApiheader;
    }
}
Pratapi Hemant Patel
  • 2,554
  • 1
  • 17
  • 29