java - 错误请求 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
我仍然不知道我在这个哈希集上做错了什么,有人可以帮忙吗?
解决方案
看起来项目为空。您可以避免它忽略类级别的空值:
@JsonInclude(Include.NON_NULL)
public class YourDTO { ... }
或在财产层面:
public class YourDTO {
@JsonInclude(Include.NON_NULL)
private String stringValue;
private int intValue;
// standard getters and setters
}
祝你好运!
推荐阅读
- angular - 如果使用 Angular 以反应形式选中复选框,则显示 div
- csv - 将 csv 导入 Nextjs 并使用 d3 读取
- google-apps-script - 从谷歌表格单元格获取文本到谷歌文档
- .net-core - 如何在 .net api 网关中使用 gRPC 服务?
- android - 当我改变位置时,我的 IP 在 Visual Studio 代码中看起来不同
- sql - 错误地将多个表添加到 masterdb
- typescript - 使用 mapbox 作为 VGeosearch 的提供者
- c# - 游戏结束后如何重新开始游戏?
- python - 访问函数中的变量而不在 Python 中调用它
- flutter - Flutter int counter...添加范围