2016-08-23 3 views
0

Ich habe ein Formular, das Liste der Kunden zurückgeben sollte.
sollte diese Form anders in zwei Fall verhalten:
Ignorieren abwesend Eigenschaft beim Mapping JSON Antwort

  1. Benutzer startet die Forschung nur "Nachname"
  2. Benutzer startet die Forschung mit Name und Vorname

In der ersten Verwendung Falls die JSON-Antwort weniger Felder hat als die Antwort im zweiten Fall, muss ich alle diese Felder ignorieren.
Ich habe versucht, mit @JsonInclude(JsonInclude.Include.NON_ABSENT), @JsonInclude(JsonInclude.Include.NON_EMPTY) und @JsonInclude(JsonInclude.Include.NON_NULL) aber mit jedem und jeder von ihnen der Fehler zurückgegeben wird, ist immer die gleiche:

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"]) 



Dies ist 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 + "]"; 
}} 



Irgendeine Idee?

EDIT: dies ist ein Beispiel für die json Antwortstruktur in Fall 1

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


Und dies ist ein Beispiel für die json Reaktion in Fall 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" 
    } 


Wie Sie sehen können, gibt es weniger Felder in Fall 1 und STS geht in den Voll-Panik-Modus ...

+0

können Sie bitte auch ein Beispiel für die Daten, die Sie analysieren möchten, posten? –

+0

versuchen Sie, Ihre Annotation zu ändern: '@JsonInclude (Include.NON_NULL)' oder '@JsonInclude (JsonSerialize.Inclusion.NON_NULL)' – Lucas

+0

diese Annotationen geben mir den gleichen Fehler: "Include/JsonSerialize kann nicht in eine Variable aufgelöst werden" –

Antwort

1

Sie müssen Ihren Objekt-Mapper so konfigurieren, dass er bei leeren Beans nicht fehlschlägt.

Hier ist ein Beispielcode, da Sie nicht die Erstellung des ObjectMapper Code selbst vorsah: mit @JsonIgnoreProperties(ignoreUnknown=true)

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

Was ist mit jacksonMapper.configure (DeSerializationFeature.FAIL_ON_UNKNOWN_PROPERTY, false); – subash

+0

Ich werde hinzufügen, danke. –

+0

Ich habe keinen ObjectMapper erstellt –

Verwandte Themen