Issue
I'm having a problem deleting a product from the db in the frontend (angular). I'm having this error:
Required request body is missing: public boolean prodcust.controller.DeleteController.deleteProduct(java.lang.String,prodcust.model.Product)]
I tested the Delete API in Postman and it works fine, the problem I'm only having in Angular when I click the Delete button.
CONTROLLER:
@RequestMapping(value = "/delete-product/{codeproduct}",method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public boolean deleteProduct(@PathVariable("codeproduct") String codeproduct, @RequestBody Product product) {
product.setCodeproduct(codeproduct);
return productDao.deleteProduct(product);
}
DAO:
public boolean deleteProduct(Product product) {
boolean status=false;
try {
jdbcTemplate.update("delete from buys where codeproduct=?", product.getCodeproduct());
status=true;
} catch (Exception e) {
e.printStackTrace();
}
return status;
}
Angular: service.ts
return this.http.delete(`${this.baseUrl}/delete-product/${codeproduct}`, { responseType: 'text' });
Solution
You have @RequestBody Product product
try to send some json object or empty json {}
from angular.
Use as below.
http.request('delete',. , `${this.baseUrl}/delete-product/${codeproduct}`, body: { } });
Answered By - S. Anushan