Leon
Leon

Reputation: 745

Ignore absent property when mapping json response

I have a form that should return list of customers.
This form should behave differently in two case:

  1. User starts the research using only "surname"
  2. User starts the research using surname AND name

In the first case the json response has less fields than the response in the second case so I have to ignore all these fields.
I've tried using @JsonInclude(JsonInclude.Include.NON_ABSENT), @JsonInclude(JsonInclude.Include.NON_EMPTY) and @JsonInclude(JsonInclude.Include.NON_NULL) but with each and everyone of these the error returned is always the same:

java.lang.Exception: Could not write content: (was java.lang.NullPointerException) (through reference chain: it.gruppoitas.itasacquire.pojo.Cliente["DATA_NASCITA"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: it.gruppoitas.itasacquire.pojo.Cliente["DATA_NASCITA"])



This is the pojo Cliente:

package it.gruppoitas.itasacquire.pojo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

@JsonInclude(JsonInclude.Include.NON_ABSENT)
public class Cliente {

@JsonProperty("TIPO_PERSONA")
private String tipoPersona;
@JsonProperty("PRO_CLIE")
private String proClie;
@JsonProperty("CODICE_FISCALE")
private String codiceFiscale;
@JsonProperty("DATA_NASCITA")
private String dataNascita;
@JsonProperty("SESSO")
private String sesso;
@JsonProperty("NOME")
private String nome;
@JsonProperty("COGNOME")
private String cognome;

public String getTipoPersona() {
    return tipoPersona;
}

public void setTipoPersona(String tipoPersona) {
    this.tipoPersona = tipoPersona;
}

public String getProClie() {
    return proClie;
}

public void setProClie(String proClie) {
    this.proClie = proClie;
}

public String getCodiceFiscale() {
    return codiceFiscale;
}

public void setCodiceFiscale(String codiceFiscale) {
    this.codiceFiscale = codiceFiscale;
}

public String getDataNascita() {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
    Date data = null;
    try {
        data = sdf.parse(dataNascita);
        dataNascita = new SimpleDateFormat("dd/MM/yyyy").format(data);
    } catch (ParseException e) {
        System.err.println(e);
    }
    return dataNascita;
}

public void setDataNascita(String dataNascita) {
    this.dataNascita = dataNascita;
}

public String getSesso() {
    return sesso;
}

public void setSesso(String sesso) {
    this.sesso = sesso;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getCognome() {
    return cognome;
}

public void setCognome(String cognome) {
    this.cognome = cognome;
}

@Override
public String toString() {
    return "Cliente [tipoPersona=" + tipoPersona + ", proClie=" + proClie + ", codiceFiscale=" + codiceFiscale + ", dataNascita="
            + dataNascita + ", sesso=" + sesso + ", nome=" + nome + ", cognome=" + cognome + "]";
}}



Any idea?

EDIT: this is an example of the json response structure in case 1

 {
  "TIPO_PERSONA" : "G",
  "PRO_CLIE" : "123456789",
  "CODICE_FISCALE" : "123456789",
  "PARTITA_IVA" : "123456789",
  "SESSO" : "S",
  "COGNOME" : "CUSTOMER SRL"
}


And this is an example of the json response in case 2:

     {  
      "TIPO_PERSONA" : "F",
      "PRO_CLIE" : "123456789",
      "CODICE_FISCALE" : "123456789",
      "DATA_NASCITA" : "1969-09-07 00:00:00.0",
      "SESSO" : "F",
      "NOME" : "Foo",
      "COGNOME" : "Fie"
    }


As you can see there are less fields in case 1 and STS goes in full-panic mode...

Upvotes: 0

Views: 5714

Answers (2)

Alptekin T.
Alptekin T.

Reputation: 115

You can also use:

jacksonMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES,false);

Upvotes: 0

Avihoo Mamka
Avihoo Mamka

Reputation: 4786

You need to configure your object mapper not to fail on empty beans.

Here is a sample code since you didn't provide the creation of the ObjectMapper code yourself:

private ObjectMapper jacksonMapper = new ObjectMapper();
jacksonMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
jacksonMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

Upvotes: 2

Related Questions