java - 我的 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")
推荐阅读
- ios - 如何在 iOS 中将堆栈视图中的第一个视图置于最前面?
- regex - 为什么我的 DOB 正则表达式不被接受?
- ios - 添加到子视图的 UIbutton 使应用程序迅速崩溃
- angular - 如何根据 mat-select 中选择的值更改日期选择器掩码
- go - 如何执行命令行
- pandas - Python - 由于在转换中捕获了标头,因此无法将字符串转换为浮点数
- javascript - 计算处理组合javascript的时间
- mysql - 在 BigQuery 中追加时忽略重复记录
- php - 如何使用 Request 类进行验证
- flash - decodeURIComponent 没有做它的工作