我的Spring框架的HttpMessageNotReadableException错误



我想发送一个对象到我的数据库,与spring框架。以下是我在控制器中的代码:

@PostMapping("/createBarangByJson")
public ResponseEntity<?> createBarangByJson(@RequestBody RequestUser body)
{
Integer cDet = crudService.simpleCreateByJson(body);
return ResponseEntity.ok(cDet);
}

}

我已经为请求类做了setter getter,下面是我在DaoImpl中的代码:

@Override
public Integer simpleCreateByJson(RequestUser reqUser)
{
String sql = "INSERT INTO public.jualbeli(nama_barang, kuantitas, harga, tanggal, created_by) VALUES (?,?,?,?,?)";
Integer result = jdbcTemplate.update(sql, reqUser.getNama_barang(), Integer.parseInt(reqUser.getKuantitas()), Integer.parseInt(reqUser.getHarga()), reqUser.getTanggal(), reqUser.getCreated_by());
return result;
}

但是当我发送这个数据时:

[
{
"nama_barang": "buku",
"kuantitas": "17",
"harga": "500000",
"tanggal": "2020-08-01",
"created_by": "adit"
}

)

错误提示:

"timestamp": 1615207817069,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException"

有谁知道怎么回事吗?

你正在发送一个JSON数组:

[{
"nama_barang": "buku",
"kuantitas": "17",
"harga": "500000",
"tanggal": "2020-08-01",
"created_by": "adit"
}]

你需要发送

{
"nama_barang": "buku",
"kuantitas": "17",
"harga": "500000",
"tanggal": "2020-08-01",
"created_by": "adit"
}

还应该要求

@PostMapping(path="/createBarangByJson", consumes="application/json") 

不是

@PostMapping("/createBarangByJson")

相关内容

  • 没有找到相关文章

最新更新