0

I need to hide some of the fields in the model class in my response object. I tried to follow this SO answer but with no luck.

when there are getter and setters for a field then the @JsonIgnore annotation doesn't seem to be working. see the following code snippet for clarifications.

@ApiModel(description = "")
public class APIInfoDTO  {

  private String id = null;
  
  @JsonIgnore //this field will not be hidden when getters and setters are defined..
  private String name = null;

  
  private String status = null;

  @JsonIgnore // this "info" field is hidden since there are no getters                                 and setters for this field 
  private String info = "adncusdvshbdvsbvhdb";

  /**
   **/
  @ApiModelProperty(value = "")
  @JsonProperty("id")
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }

  
  /**
   **/
  @ApiModelProperty(value = "")
  @JsonProperty("name")
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }

  
  /**
   **/


@ApiModelProperty(value = "")
  @JsonIgnore
  public String getDescription() {
    return description;
  }
  @JsonProperty("description")
  public void setDescription(String description) {
    this.description = description;
  }

furthermore this is the code snippet for object mapping

public static APIInfoDTO fromAPIToInfoDTO(API api) {
    APIInfoDTO apiInfoDTO = new APIInfoDTO();
    apiInfoDTO.setDescription(api.getDescription());
    apiInfoDTO.setContext(api.getContext());
    apiInfoDTO.setId(api.getUUID());
    APIIdentifier apiId = api.getId();
    apiInfoDTO.setName(apiId.getApiName());
    apiInfoDTO.setVersion(apiId.getVersion());
    apiInfoDTO.setProvider(apiId.getProviderName());
    apiInfoDTO.setStatus(api.getStatus().toString());
    String providerName = api.getId().getProviderName();
    apiInfoDTO.setProvider(APIUtil.replaceEmailDomainBack(providerName));
    return apiInfoDTO;
}

any helpful answer would be highly appreciated.. Thanks

[UPDATE] The @JsonIgnore works with org.codehaus.jackson:jackson-core-asl:1.8.6 but fails with com.fasterxml.jackson.core:jackson-annotations:2.7.2.. Any idea why???

Community
  • 1
  • 1
Infamous
  • 744
  • 11
  • 25

2 Answers2

0

Add @JsonIgnore Annotation to the getter method as well.

Or Try adding @JsonIgnoreProperties(value={"name"}) at Class level, if this is an option for you

UPDATE

If you have Proper Jackson Library in your classpath (group: 'com.fasterxml.jackson.core', name: 'jackson-core'), @JsonIgnore on your field will work just fine; as long as the getter method you have is a standard getter, you don't have to annotate getter with @JsonIgnore.

so-random-dude
  • 15,277
  • 10
  • 68
  • 113
  • 1
    No it doesn't work.. I updated the Q as well, I applied the @JsonIgnore to getter but still it doesn't work – Infamous Oct 05 '16 at 05:10
  • Your getName() is annotated with @JsonProperty("name"). Did you try removing that and adding @JsonIgnore instead. This served the purpose for me in a Spring boot application – so-random-dude Oct 05 '16 at 05:13
  • Dude I need to keep the name intact but drop the Info field and i have tried that but that doesnt work as well – Infamous Oct 05 '16 at 05:36
  • @Infamous If this is an option for you , try adding @JsonIgnoreProperties(value={"name"}) at Class level – so-random-dude Oct 05 '16 at 06:17
  • @Infamous .. So , JsonIgnore Annotation to the getter method did work? – so-random-dude Oct 05 '16 at 07:08
  • 1
    yeah it works only if the annotation is taken from org.codehaus.jackson:jackson-core which is bit old and AFAIU discontinued. I want JsonIgnore to work with com.fasterxml.jackson.core:jackson-annotations – Infamous Oct 05 '16 at 08:34
  • @Infamous Equivalent of jackson-core-asl is com.fasterxml.jackson.core:jackson-core, just try with this library instead. – so-random-dude Oct 05 '16 at 16:22
-1

If you want to serialize and deserialize your object based only on fields annotations, the Jackson ObjectMapper should be configured to ignore getters and setters method:

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker()
                .withFieldVisibility(JsonAutoDetect.Visibility.ANY)
                .withGetterVisibility(JsonAutoDetect.Visibility.NONE)
                .withSetterVisibility(JsonAutoDetect.Visibility.NONE));

or

ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);

It can also be configured at Class level using the @JsonAutoDetect annotation.

@JsonAutoDetect(fieldVisibility = Visibility.ANY, getterVisibility = Visibility.NONE, setterVisibility = Visibility.NONE)
public class APIInfoDTO  {
    // ...
}
Cristian Greco
  • 2,566
  • 1
  • 19
  • 24