exception : spring - ResponseStatusExceptionResolver

This commit is contained in:
haerong22
2021-07-26 17:51:08 +09:00
parent c8ca1c2c6a
commit 2706e2f16d
4 changed files with 23 additions and 1 deletions

View File

@@ -1,12 +1,15 @@
package hello.exception.api;
import hello.exception.exception.BadRequestException;
import hello.exception.exception.UserException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
@Slf4j
@RestController
@@ -27,6 +30,16 @@ public class ApiExceptionController {
return new MemberDto(id, "hello " + id);
}
@GetMapping("/api/response-status-ex1")
public String responseStatusEx1() {
throw new BadRequestException();
}
@GetMapping("/api/response-status-ex2")
public String responseStatusEx2() {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "error.bad", new IllegalArgumentException());
}
@Data
@AllArgsConstructor

View File

@@ -0,0 +1,8 @@
package hello.exception.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.BAD_REQUEST, reason = "error.bad")
public class BadRequestException extends RuntimeException{
}

View File

@@ -6,6 +6,6 @@ server.error.whitelabel.enabled=true
# always :항상 사용
# on_param : 파라미터가 있을 때 사용
server.error.include-exception=true
server.error.include-message=on_param
server.error.include-message=always
server.error.include-stacktrace=on_param
server.error.include-binding-errors=on_param

View File

@@ -0,0 +1 @@
error.bad=잘못된 요청 오류 입니다.