1

I am trying to ignore a property while deserialization, but not serialization.

I have a property(specType) which is not required for the client to send but while retrieval client should be able to see that property. Means, while GET they should be able to see but while POST they should not see specType.

I tried following combinations but non of them worked. I am using jackson-annotaion 2.9.0

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonProperty.Access;

public class A1 {

    @JsonProperty(access = Access.READ_ONLY)
    private String specType;

    public String getSpecType() {
        return specType;
    }

    @JsonIgnore
    public void setSpecType(String specType) {
        this.specType = specType;
    }


}

==

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

public class A1 {

    @JsonIgnore
    private String specType;

    @JsonProperty("specType")
    public String getSpecType() {
        return specType;
    }

    @JsonIgnore
    public void setSpecType(String specType) {
        this.specType = specType;
    }

}

==

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(value = { "specType" }, allowGetters = true)
public class A1 {

    private String specType;

    public String getSpecType() {
        return specType;
    }

    public void setSpecType(String specType) {
        this.specType = specType;
    }

}

I am using swagger following swagger versions: springfox-swagger-ui version 2.9.2 springfox-swagger2 version 2.9.2 Am I missing anything?

Shashi Ranjan
  • 87
  • 1
  • 10
  • As noted adding `@JsonProperty(access = Access.READ_ONLY)` **and only this** (e.g. remove the `@JsonIgnore` on the setter) should work. – Alan Hay Jun 21 '19 at 07:57
  • Opposite of what you asked: https://stackoverflow.com/q/12505141/2987755 – dkb Jun 21 '19 at 08:36
  • @dkb Oposite to that only I tried. But no luck till now. – Shashi Ranjan Jun 21 '19 at 08:56
  • @AlanHay Only `@JsonProperty(access = Access.READ_ONLY)` also did't worked for me. – Shashi Ranjan Jun 21 '19 at 09:12
  • 2
    is your question around generation of swagger docs as opposed to the actual functionality? – Alan Hay Jun 21 '19 at 09:28
  • works for me `A1 a1 = new A1();` `a1.setSpecType("Hello");` `a1.setId(1);` `String str = objectMapper.writeValueAsString(a1);` `System.out.println(str);// {"id":1,"specType":"Hello"}` `A1 a2 = objectMapper.readValue(str, A1.class);` `System.out.println(a2.getId() +":"+a2.getSpecType()); // 1 : null` – dkb Jun 21 '19 at 09:34
  • As you can see above, I am able to serialize `specType` but during deserialization, `specType` is `null` – dkb Jun 21 '19 at 09:35
  • @AlanHay Yes! This question is regarding generation of swagger docs only. – Shashi Ranjan Jun 21 '19 at 10:59
  • **CAN YOU MAKE THAT CLEAR IN THE TITLE AND TEXT OF YOUR QUESTION THEN!** – Alan Hay Jun 21 '19 at 11:01

0 Answers0