httpMessageNoteableException错误

o2g1uqev  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(258)

我想用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"

有人知道怎么了吗?

uubf1zoe

uubf1zoe1#

您正在发送一个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")

相关问题