From 87b3839fe789ce6c46937a0ede2e1f437dbc3623 Mon Sep 17 00:00:00 2001 From: leebj Date: Thu, 18 Jul 2024 17:47:57 +0900 Subject: [PATCH] =?UTF-8?q?=EC=98=A4=EB=A5=98=EB=B0=9C=EC=83=9D=EC=8B=9C?= =?UTF-8?q?=20=EC=88=98=EC=8B=A0=EB=B0=9B=EB=8A=94=20HTTP=EC=83=81?= =?UTF-8?q?=ED=83=9C=EC=BD=94=EB=93=9C=EB=A5=BC=20=EC=A7=80=EC=A0=95?= =?UTF-8?q?=ED=95=98=EA=B3=A0=20=EB=A9=94=EC=8B=9C=EC=A7=80=EB=A5=BC=20?= =?UTF-8?q?=ED=85=8D=EC=8A=A4=ED=8A=B8=EB=A1=9C=20=EC=9D=91=EB=8B=B5?= =?UTF-8?q?=EB=B0=9B=EB=8A=94=20=ED=81=B4=EB=9E=98=EC=8A=A4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../xit/applib/HttpStatusCodeException.java | 17 ++++++++++++ ...tpStatusCodeExceptionControllerAdvice.java | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/cokr/xit/applib/HttpStatusCodeException.java create mode 100644 src/main/java/cokr/xit/applib/HttpStatusCodeExceptionControllerAdvice.java diff --git a/src/main/java/cokr/xit/applib/HttpStatusCodeException.java b/src/main/java/cokr/xit/applib/HttpStatusCodeException.java new file mode 100644 index 0000000..263a6f4 --- /dev/null +++ b/src/main/java/cokr/xit/applib/HttpStatusCodeException.java @@ -0,0 +1,17 @@ +package cokr.xit.applib; + +public class HttpStatusCodeException extends RuntimeException { + + private static final long serialVersionUID = 1L; + + private int status; + + public int getStatus() { + return this.status; + } + + public HttpStatusCodeException(int statusCode, String message){ + super(message); + this.status = statusCode; + } +} diff --git a/src/main/java/cokr/xit/applib/HttpStatusCodeExceptionControllerAdvice.java b/src/main/java/cokr/xit/applib/HttpStatusCodeExceptionControllerAdvice.java new file mode 100644 index 0000000..004e0e9 --- /dev/null +++ b/src/main/java/cokr/xit/applib/HttpStatusCodeExceptionControllerAdvice.java @@ -0,0 +1,27 @@ +package cokr.xit.applib; + +import java.nio.charset.StandardCharsets; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.context.request.WebRequest; +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; + +@ControllerAdvice +public class HttpStatusCodeExceptionControllerAdvice extends ResponseEntityExceptionHandler { + + @ExceptionHandler(HttpStatusCodeException.class) + public ResponseEntity handleHttpStatusCodeException( + final HttpStatusCodeException exception, final WebRequest webRequest) { + int status = exception.getStatus(); + String body = exception.getMessage(); + HttpHeaders header = new HttpHeaders(); + header.setContentType(new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8)); + return handleExceptionInternal(exception, body, header, HttpStatus.valueOf(status), webRequest); + } + +}