restapi
-
[Restful 학습] - PathVariableSPRING/WEBSERVICE 2020. 8. 1. 13:18
아래와 같이 PathVariable로 변수를 받을 시에는 URL PATH에 따라 값을 설정할 수 있다. @GetMapping(path = "/hello-world-bean/path-variable/{name}") public HelloWorldBean helloWolrdBean(@PathVariable String name) { return new HelloWorldBean(String.format("Hello World %s", name)); }
-
[RestFul 학습] - EXCEPTION관리SPRING/WEBSERVICE 2020. 5. 28. 23:50
Rest Api에서 아래와 같이 id에 따른 List를 불러오려고 할 때 user 값이 존재하지 않으면 Exception을 일으키는 방식으로 response 를 내려줄 수 있다. // GET /users/1 or /users/10 -> String @GetMapping("/users/{id}") public User retrieveUser(@PathVariable int id) { User user =service.findOne(id); if (user == null) { throw new UserNotFoundException(String.format("ID[%s] not found", id)); } return user; } 아래와 같이 @ResponseStatus에 404 코드를 세팅하면 Http ..