January 2018
Intermediate to advanced
414 pages
10h 29m
English
According to our definition, initially, when we get a GET request for a resource we should answer with the resource and a 200 OK or with 404 NOT FOUND if the resource is not found.
So, we can modify our method:
@GetMapping(value = "/customer/{id}")fun getCustomer(@PathVariable id: Int): ResponseEntity<Customer?> { val customer = customerService.getCustomer(id) val status = if (customer == null) HttpStatus.NOT_FOUND else HttpStatus.OK return ResponseEntity(customer, status)}
First, we get the customer; if the customer is not found, the service will return null, so we can set our status to 404 NOT FOUND or 200 OK.
Read now
Unlock full access