Spring Boot/Restful-Api + Spring Boot
RestFul API 연습(5) 회원관리 예외처리 포스트맨사용
seogi8574
2022. 6. 2. 18:29
반응형
예를 들면 1~3번 까지 기존 데이터가 있는데
100번 데이터를 입력 했다고 가정해보자
그러면 오류가 날 것이다.
이때 예외처리를 할 것이다.
이전 DeleteUser에서
if(user == null){
throw new UserNotFoundExeception(String.format(id + " not pound"));
}
이것을 하기위해
UsernotFoundException이라는 클래스를 만들어 보자
package com.example.restfulwebservice.user;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
// 2xx -> ok
// 4xx -> 클라이언트가 잘못
// 5xx -> 서버가 잘못
@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotFoundExeception extends RuntimeException {
public UserNotFoundExeception(String message) {
super(message);
}
}
ResponseStatus
을 사용하여 Not Foind를 선택해준다.
이것은 500에러를 404에러로 변경해주기도 한다.
그 후 아까 지정한
user가 없으면 메시지를 출력해준다.
if(user == null){
throw new UserNotFoundExeception(String.format(id + " not pound"));
}
100을 입력하면 100은 없다고 메시지가 출력된다.

반응형