简体   繁体   English

映射json响应时忽略缺少的属性

[英]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. 在第一种情况下,json响应的字段少于第二种情况下的响应,因此我必须忽略所有这些字段。
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: 我试过使用@JsonInclude(JsonInclude.Include.NON_ABSENT)@JsonInclude(JsonInclude.Include.NON_EMPTY)@JsonInclude(JsonInclude.Include.NON_NULL)但对于每个返回的错误,总是相同的:

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: 这是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 编辑:这是案例1中json响应结构的示例

 {
  "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: 这是案例2中json响应的示例:

     {  
      "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... 如您所见,情况1的字段减少了,STS进入全恐慌模式...

You need to configure your object mapper not to fail on empty beans. 您需要配置对象映射器,使其在空bean上不失败。

Here is a sample code since you didn't provide the creation of the ObjectMapper code yourself: 这是示例代码,因为您没有自己提供ObjectMapper代码的创建:

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

您还可以使用:

jacksonMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES,false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM