|
| 1 | +package com.baeldung.execption; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Autowired; |
| 4 | +import org.springframework.http.HttpStatus; |
| 5 | +import org.springframework.web.bind.annotation.DeleteMapping; |
| 6 | +import org.springframework.web.bind.annotation.GetMapping; |
| 7 | +import org.springframework.web.bind.annotation.PathVariable; |
| 8 | +import org.springframework.web.bind.annotation.PutMapping; |
| 9 | +import org.springframework.web.bind.annotation.RestController; |
| 10 | +import org.springframework.web.server.ResponseStatusException; |
| 11 | + |
| 12 | +@RestController |
| 13 | +public class ActorController { |
| 14 | + |
| 15 | + @Autowired |
| 16 | + ActorService actorService; |
| 17 | + |
| 18 | + @GetMapping("/actor/{id}") |
| 19 | + public String getActorName(@PathVariable("id") int id) { |
| 20 | + try { |
| 21 | + return actorService.getActor(id); |
| 22 | + } catch (ActorNotFoundException ex) { |
| 23 | + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Actor Not Found", ex); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + @DeleteMapping("/actor/{id}") |
| 28 | + public String getActor(@PathVariable("id") int id) { |
| 29 | + return actorService.removeActor(id); |
| 30 | + } |
| 31 | + |
| 32 | + @PutMapping("/actor/{id}/{name}") |
| 33 | + public String updateActorName(@PathVariable("id") int id, @PathVariable("name") String name) { |
| 34 | + try { |
| 35 | + return actorService.updateActor(id, name); |
| 36 | + } catch (ActorNotFoundException ex) { |
| 37 | + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Provide correct Actor Id", ex); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | +} |
0 commit comments