6

I need to exposes a property in my json that will be processed in the getter method.

The class:

public class Configuracao{

   private String departamento;

   public String getDepartamento(){/**getter code**/}

   public void setDepartamento(String departamento){/**setter code**/}

   public String getDepartamentos(){/***Some logic code***/}

}

The json that got in front: {departamento: "Lote", departamentos: "Lotes"}

Works fine in serialization, but when my front-end post the json back, jackson throws a unrecognized field exception caused by 'departamentos'. How can I tell that I just want to 'departamentos' be serialized by the method value and be ignored in deserialization. I tried @JsonIgnoreProperty, @JsonGetter and @JsonProperty(access = JsonProperty.Access.READ_ONLY) on the method but nothing works.

josev.junior
  • 409
  • 2
  • 5
  • 13

2 Answers2

4

You can use JsonIgnoreProperties annotation:

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.concurrent.ThreadLocalRandom;

public class JsonPathApp {

    public static void main(String[] args) throws Exception {
        Configuracao c = new Configuracao();
        c.setDepartamento("D1");

        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(c);
        System.out.println(json);
        System.out.println(mapper.readValue(json, Configuracao.class));
    }
}

@JsonIgnoreProperties(ignoreUnknown = true)
class Configuracao {

    private String departamento;

    public String getDepartamento() {
        return departamento;
    }

    public void setDepartamento(String departamento) {
        this.departamento = departamento;
    }

    public String getDepartamentos() {
        return departamento + " " + ThreadLocalRandom.current().nextDouble();
    }

    @Override
    public String toString() {
        return "Configuracao{" +
                "departamento='" + departamento + '\'' +
                '}';
    }
}

Above code prints:

{"departamento":"D1","departamentos":"D1 0.8600092703789755"}
Configuracao{departamento='D1'}

JsonProperty.Access.READ_ONLY should also works:

class Configuracao {

    private String departamento;

    public String getDepartamento() {
        return departamento;
    }

    public void setDepartamento(String departamento) {
        this.departamento = departamento;
    }

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    public String getDepartamentos() {
        return departamento + " " + ThreadLocalRandom.current().nextDouble();
    }

    @Override
    public String toString() {
        return "Configuracao{" +
                "departamento='" + departamento + '\'' +
                '}';
    }
}

with above test works as expected.

If you have more classes like this and fields to ignore, you can disable globally feature DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES:

ObjectMapper mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);

Everything was tested with version 2.9.9

Michał Ziober
  • 37,175
  • 18
  • 99
  • 146
  • `ignoreUnknown = true` did the trick but `JsonProperty.Access.READ_ONLY` didn't. I expected jackson should have a manner to handle this situation without ignore annotations. But that's it for today. – josev.junior Oct 27 '19 at 01:13
  • @josev.junior, Which version of `Jackson` do yo use? In version `2.9.9` all shown solution work properly. – Michał Ziober Oct 27 '19 at 12:22
0

Just define departamentos property in Configuracao class.

public class Configuracao{
   private String departamento;
   private String departamentos;
   //omitted getter/setter
}
Holinc
  • 641
  • 6
  • 17
  • That's is the only way to solve? I didn't want to create a `departamentos` property because it is the processed `departamento`. I just want to serialize (and not deserialize) without define the property. – josev.junior Oct 26 '19 at 15:00