Validate empty request

89 views Asked by At

I am developing a SpringBoot Application. For a POST end-point, there is NO request body to be passed. My service is working fine. But its also working when I pass some values in the body of the request. How can I validate and return a BAD REQUEST if something is passed in the body of request which should ideally remain empty for this request?

*@RequestMapping(value = "/sample/customers", method = POST)
public Customer session() {
    return customerService.getCustomer();
}*
1

There are 1 answers

0
Dzinot On

You should not use POST for data retrieval.

You'll need 2 methods. Here is an example code for the save:

@ResponseStatus(value = HttpStatus.CREATED)
@RequestMapping(value = "/sample/customers", method = POST)
public Customer save(@RequestBody CustomerRequestDto customerRequestDto) {
    return customerService.save(customerRequestDto);
}