ControllerAdvice
-
[Restuful 학습] - Exception 핸들링을 통한 HTTP STATUS Code 관리법 (ControllerAdvice 사용)SPRING/WEBSERVICE 2020. 8. 4. 00:07
package com.example.demo.exception; @RestController @ControllerAdvice public class CustomizedResponseEntityExceptionHandler extends ResponseEntityExceptionHandler{ @ExceptionHandler(Exception.class) public final ResponseEntity handleAllExcetipon(Exception ex, WebRequest request) { System.out.println("Exception..."); ExceptionResponse exceptionResponse = new ExceptionResponse(new Date(), ex.getMe..
-
[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 ..