{"id":1462,"date":"2020-11-12T21:58:39","date_gmt":"2020-11-12T12:58:39","guid":{"rendered":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=1462"},"modified":"2023-02-19T11:26:56","modified_gmt":"2023-02-19T02:26:56","slug":"controlleradvice","status":"publish","type":"post","link":"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=1462","title":{"rendered":"ControllerAdvice"},"content":{"rendered":"<h1>ControllerAdvice<\/h1>\n<h2>ControllerAdvice vs RestControllerAdvice<\/h2>\n<p>\ubaa8\ub450 \ubaa8\ub4e0 \ucee8\ud2b8\ub864\ub7ec(Controller + RestController) \uc758 \uc608\uc678\ub97c \uc7a1\uc2b5\ub2c8\ub2e4.<br \/>\n\ucc28\uc774\uc810\uc740 \ub9ac\ud134\ub370\uc774\ud0c0\uac00 \uc788\ub294\uac00 \uc5c6\ub294\uac00 \ubfd0\uc785\ub2c8\ub2e4.<\/p>\n<h2>\uc8fc\uc758\uc810<\/h2>\n<p>ControllerAdvice \uac00 \ud504\ub85c\uc81d\ud2b8\uc5d0 \uc5ec\ub7ec\uac1c\uc778 \uacbd\uc6b0 Spring \uc740 \ub79c\ub364\ud55c \uc21c\uc11c\ub85c ControllerAdvice \ub97c \ud655\uc778\ud569\ub2c8\ub2e4.<br \/>\nControllerAdvice \uc758 \uc21c\uc11c\ub97c \uace0\uc815\ud574 \uc8fc\uac70\ub098 \ud504\ub85c\uc81d\ud2b8\uc5d0 \ud558\ub098\uc758 ControllerAdvice \ub9cc \ub450\ub294 \uac83\uc774 \uc88b\uc2b5\ub2c8\ub2e4.<\/p>\n<h2>RestControllerAdvice<\/h2>\n<p>\ucee8\ud2b8\ub864\ub7ec\uc5d0\uc11c \ubc1c\uc0dd\ud558\ub294 \uc624\ub958\ub97c \ud1b5\ud569\ud574\uc11c \uad00\ub9ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<\/p>\n<pre><code class=\"language-java\">public class DataNotFoundException extends RuntimeException {\n    public DataNotFoundException(String message) {\n        super(message);\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@ToString\n@Getter\npublic class ApiResponseMessage implements Serializable {\n    private final String code;\n    private final String message;\n\n    public ApiResponseMessage(ResponseCode responseCode) {\n        this.code = responseCode.getKey();\n        this.message = responseCode.getTitle();\n    }\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@Getter\n@RequiredArgsConstructor\npublic enum ResponseCode implements Serializable {\n\n    OK(&quot;000&quot;, &quot;OK&quot;),\n    DATA_NOT_FOUND(&quot;404&quot;, &quot;\ub370\uc774\ud0c0\uac00 \uc874\uc7ac\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.&quot;),\n    UNKNOWN_ERROR(&quot;999&quot;, &quot;\uc54c \uc218 \uc5c6\ub294 \uc624\ub958\uac00 \ubc1c\uc0dd\ud588\uc2b5\ub2c8\ub2e4.&quot;);\n\n    private final String key;\n    private final String title;\n}<\/code><\/pre>\n<pre><code class=\"language-java\">@RequiredArgsConstructor\n@Order(Ordered.HIGHEST_PRECEDENCE)\n@RestControllerAdvice\npublic class ExceptionAdvice {\n\n    \/\/ \ub2f9\uc5f0\ud55c \uc774\uc57c\uae30\uc9c0\ub9cc, ControllerAdvice \uac00 \ucc98\ub9ac\ud558\ub294 Exception \uc740\n    \/\/ \ucee8\ud2b8\ub864\ub7ec\uc5d0\uc11c \ubc1c\uc0dd\ud55c Exception \ub9cc\uc774\ub2e4.\n    \/\/ Spring Security \uc640 \uac19\uc774 \ucee8\ud2b8\ub864\ub7ec \uc0c1\uc704\uc5d0\uc11c \ubc1c\uc0dd\ud558\ub294 Exception \uc740 \ucc98\ub9ac\ud558\uc9c0 \uc54a\ub294\ub2e4.\n\n    @ExceptionHandler(value = {DataNotFoundException.class})\n    protected ResponseEntity&lt;?&gt; handleDataNotFoundException(DataNotFoundException e) {\n        return new ResponseEntity&lt;&gt;(new ApiResponseMessage(ResponseCode.DATA_NOT_FOUND), HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n\n    @ExceptionHandler(Exception.class)\n    protected ResponseEntity&lt;?&gt; handleException(Exception e) {\n        return new ResponseEntity&lt;&gt;(HttpStatus.INTERNAL_SERVER_ERROR);\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>ControllerAdvice ControllerAdvice vs RestControllerAdvice \ubaa8\ub450 \ubaa8\ub4e0 \ucee8\ud2b8\ub864\ub7ec(Controller + RestController) \uc758 \uc608\uc678\ub97c \uc7a1\uc2b5\ub2c8\ub2e4. \ucc28\uc774\uc810\uc740 \ub9ac\ud134\ub370\uc774\ud0c0\uac00 \uc788\ub294\uac00 \uc5c6\ub294\uac00 \ubfd0\uc785\ub2c8\ub2e4. \uc8fc\uc758\uc810 ControllerAdvice \uac00 \ud504\ub85c\uc81d\ud2b8\uc5d0 \uc5ec\ub7ec\uac1c\uc778 \uacbd\uc6b0 Spring \uc740 \ub79c\ub364\ud55c \uc21c\uc11c\ub85c ControllerAdvice \ub97c \ud655\uc778\ud569\ub2c8\ub2e4. ControllerAdvice \uc758 \uc21c\uc11c\ub97c \uace0\uc815\ud574 \uc8fc\uac70\ub098 \ud504\ub85c\uc81d\ud2b8\uc5d0 \ud558\ub098\uc758 ControllerAdvice \ub9cc \ub450\ub294 \uac83\uc774 \uc88b\uc2b5\ub2c8\ub2e4. RestControllerAdvice \ucee8\ud2b8\ub864\ub7ec\uc5d0\uc11c \ubc1c\uc0dd\ud558\ub294 \uc624\ub958\ub97c \ud1b5\ud569\ud574\uc11c \uad00\ub9ac\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4. public class DataNotFoundException extends RuntimeException\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.skyer9.pe.kr\/wordpress\/?p=1462\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1462","post","type-post","status-publish","format-standard","hentry","category-spring-boot-2-1"],"_links":{"self":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1462","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1462"}],"version-history":[{"count":6,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1462\/revisions"}],"predecessor-version":[{"id":7624,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1462\/revisions\/7624"}],"wp:attachment":[{"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1462"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1462"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skyer9.pe.kr\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1462"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}