首页 > 解决方案 > 错误请求 JSON

问题描述

当我/get在端点上发出请求时,我遇到了这个问题:

"status": 400,
"error": "Bad Request",
"message": "JSON parse error: (was java.lang.NullPointerException);
           nested exception is com.fasterxml.jackson.databind.JsonMappingException:
           (was java.lang.NullPointerException) (through reference chain:
           com.santosglaiton.cursomc.domain.Pedido[\"itens\"]->java.util.HashSet[1])"

我的域类是这个:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;

@JsonFormat(pattern = "dd/MM/yyyy HH:mm")
private Date instante;

@OneToOne(cascade = CascadeType.ALL, mappedBy = "pedido")
private Pagamento pagamento;

@ManyToOne
@JoinColumn(name = "cliente_id")
private Cliente cliente;

@ManyToOne
@JoinColumn(name = "endereco_de_entrega_id")
private Endereco enderecoDeEntrega;

@OneToMany(mappedBy = "id.pedido")
private Set<ItemPedido> itens = new HashSet<>();

public Pedido() {
}

public Pedido(Integer id, Date instante, Cliente cliente, Endereco enderecoDeEntrega) {
    this.id = id;
    this.instante = instante;
    this.cliente = cliente;
    this.enderecoDeEntrega = enderecoDeEntrega;
}

public double getValorTotal(){
    Double soma = 0.0;
    for(ItemPedido ip : itens){
        soma = soma + ip.getSubtotal();
    }
    return soma;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public Date getInstante() {
    return instante;
}

public void setInstante(Date instante) {
    this.instante = instante;
}

public Pagamento getPagamento() {
    return pagamento;
}

public void setPagamento(Pagamento pagamento) {
    this.pagamento = pagamento;
}

public Cliente getCliente() {
    return cliente;
}

public void setCliente(Cliente cliente) {
    this.cliente = cliente;
}

public Endereco getEnderecoDeEntrega() {
    return enderecoDeEntrega;
}

public void setEnderecoDeEntrega(Endereco enderecoDeEntrega) {
    this.enderecoDeEntrega = enderecoDeEntrega;
}

public Set<ItemPedido> getItens() {
    return itens;
}

public void setItens(Set<ItemPedido> itens) {
    this.itens = itens;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    Pedido that = (Pedido) o;
    return id.equals(that.id);
}

@Override
public int hashCode() {
    return Objects.hash(id);
}

@Override
public String toString() {
    NumberFormat nf = NumberFormat.getCurrencyInstance(new Locale("pt", "BR"));
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
    StringBuilder builder = new StringBuilder();
    builder.append("Pedido número: ");
    builder.append(getId());
    builder.append(", Instante: ");
    builder.append(sdf.format(getInstante()));
    builder.append(", Cliente: ");
    builder.append(getCliente().getNome());
    builder.append(", Situação do pagamento: ");
    builder.append(getPagamento().getEstado().getDescricao());
    builder.append("\nDetalhes:\n");
    for (ItemPedido ip : getItens()) {
        builder.append(ip.toString());
    }
    builder.append("Valor total: ");
    builder.append(nf.format(getValorTotal()));
    return builder.toString();
}

}

这是GitHub存储库:https ://github.com/santosglaiton/ProjetoLojaFullStack

我仍然不知道我在这个哈希集上做错了什么,有人可以帮忙吗?

标签: javaspring

解决方案


看起来项目为空。您可以避免它忽略类级别的空值:

@JsonInclude(Include.NON_NULL)
public class YourDTO { ... }

或在财产层面:

public class YourDTO {
 
    @JsonInclude(Include.NON_NULL)
    private String stringValue;
 
    private int intValue;
 
    // standard getters and setters
}

祝你好运!


推荐阅读