{"id":171,"date":"2024-12-05T12:49:36","date_gmt":"2024-12-05T17:49:36","guid":{"rendered":"https:\/\/coding101.xyz\/?p=171"},"modified":"2024-12-05T12:58:58","modified_gmt":"2024-12-05T17:58:58","slug":"controller-advice-springboot","status":"publish","type":"post","link":"https:\/\/coding101.xyz\/?p=171","title":{"rendered":"Controller Advice SpringBoot"},"content":{"rendered":"\n<p>Here is how to implement ControllerAdvice in SpringBoot<\/p>\n\n\n\n<!--more-->\n\n\n\n<!-- HTML generated using hilite.me --><div style=\"background: #ffffff; overflow:auto;width:auto;border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;\"><pre style=\"margin: 0; line-height: 125%\"><span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.http.HttpStatus<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.http.ResponseEntity<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.web.bind.annotation.ControllerAdvice<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.web.bind.annotation.ExceptionHandler<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">org.springframework.web.context.request.WebRequest<\/span><span style=\"color: #333333\">;<\/span>\n\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">java.util.HashMap<\/span><span style=\"color: #333333\">;<\/span>\n<span style=\"color: #008800; font-weight: bold\">import<\/span> <span style=\"color: #0e84b5; font-weight: bold\">java.util.Map<\/span><span style=\"color: #333333\">;<\/span>\n\n<span style=\"color: #555555; font-weight: bold\">@ControllerAdvice<\/span>\n<span style=\"color: #008800; font-weight: bold\">public<\/span> <span style=\"color: #008800; font-weight: bold\">class<\/span> <span style=\"color: #BB0066; font-weight: bold\">GlobalExceptionHandler<\/span> <span style=\"color: #333333\">{<\/span>\n\n    <span style=\"color: #555555; font-weight: bold\">@ExceptionHandler<\/span><span style=\"color: #333333\">(<\/span>FirstException<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">class<\/span><span style=\"color: #333333\">)<\/span>\n    <span style=\"color: #008800; font-weight: bold\">public<\/span> ResponseEntity<span style=\"color: #333333\">&lt;<\/span>Map<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;&gt;<\/span> handleFirstException<span style=\"color: #333333\">(<\/span>FirstException ex<span style=\"color: #333333\">,<\/span> WebRequest request<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\n        Map<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;<\/span> response <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> HashMap<span style=\"color: #333333\">&lt;&gt;();<\/span>\n        response<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;error&quot;<\/span><span style=\"color: #333333\">,<\/span> <span style=\"background-color: #fff0f0\">&quot;FirstException occurred&quot;<\/span><span style=\"color: #333333\">);<\/span>\n        response<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;message&quot;<\/span><span style=\"color: #333333\">,<\/span> ex<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">getMessage<\/span><span style=\"color: #333333\">());<\/span>\n        <span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> ResponseEntity<span style=\"color: #333333\">&lt;&gt;(<\/span>response<span style=\"color: #333333\">,<\/span> HttpStatus<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">BAD_REQUEST<\/span><span style=\"color: #333333\">);<\/span>\n    <span style=\"color: #333333\">}<\/span>\n\n    <span style=\"color: #555555; font-weight: bold\">@ExceptionHandler<\/span><span style=\"color: #333333\">(<\/span>SecondException<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">class<\/span><span style=\"color: #333333\">)<\/span>\n    <span style=\"color: #008800; font-weight: bold\">public<\/span> ResponseEntity<span style=\"color: #333333\">&lt;<\/span>Map<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;&gt;<\/span> handleSecondException<span style=\"color: #333333\">(<\/span>SecondException ex<span style=\"color: #333333\">,<\/span> WebRequest request<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\n        Map<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;<\/span> response <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> HashMap<span style=\"color: #333333\">&lt;&gt;();<\/span>\n        response<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;error&quot;<\/span><span style=\"color: #333333\">,<\/span> <span style=\"background-color: #fff0f0\">&quot;SecondException occurred&quot;<\/span><span style=\"color: #333333\">);<\/span>\n        response<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;message&quot;<\/span><span style=\"color: #333333\">,<\/span> ex<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">getMessage<\/span><span style=\"color: #333333\">());<\/span>\n        <span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> ResponseEntity<span style=\"color: #333333\">&lt;&gt;(<\/span>response<span style=\"color: #333333\">,<\/span> HttpStatus<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">INTERNAL_SERVER_ERROR<\/span><span style=\"color: #333333\">);<\/span>\n    <span style=\"color: #333333\">}<\/span>\n\n    <span style=\"color: #555555; font-weight: bold\">@ExceptionHandler<\/span><span style=\"color: #333333\">(<\/span>Exception<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">class<\/span><span style=\"color: #333333\">)<\/span>\n    <span style=\"color: #008800; font-weight: bold\">public<\/span> ResponseEntity<span style=\"color: #333333\">&lt;<\/span>Map<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;&gt;<\/span> handleAllOtherExceptions<span style=\"color: #333333\">(<\/span>Exception ex<span style=\"color: #333333\">,<\/span> WebRequest request<span style=\"color: #333333\">)<\/span> <span style=\"color: #333333\">{<\/span>\n        Map<span style=\"color: #333333\">&lt;<\/span>String<span style=\"color: #333333\">,<\/span> String<span style=\"color: #333333\">&gt;<\/span> response <span style=\"color: #333333\">=<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> HashMap<span style=\"color: #333333\">&lt;&gt;();<\/span>\n        response<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;error&quot;<\/span><span style=\"color: #333333\">,<\/span> <span style=\"background-color: #fff0f0\">&quot;Unexpected error&quot;<\/span><span style=\"color: #333333\">);<\/span>\n        response<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">put<\/span><span style=\"color: #333333\">(<\/span><span style=\"background-color: #fff0f0\">&quot;message&quot;<\/span><span style=\"color: #333333\">,<\/span> ex<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">getMessage<\/span><span style=\"color: #333333\">());<\/span>\n        <span style=\"color: #008800; font-weight: bold\">return<\/span> <span style=\"color: #008800; font-weight: bold\">new<\/span> ResponseEntity<span style=\"color: #333333\">&lt;&gt;(<\/span>response<span style=\"color: #333333\">,<\/span> HttpStatus<span style=\"color: #333333\">.<\/span><span style=\"color: #0000CC\">INTERNAL_SERVER_ERROR<\/span><span style=\"color: #333333\">);<\/span>\n    <span style=\"color: #333333\">}<\/span>\n<span style=\"color: #333333\">}<\/span>\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here is how to implement ControllerAdvice in SpringBoot<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=171"}],"version-history":[{"count":2,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/171\/revisions"}],"predecessor-version":[{"id":173,"href":"https:\/\/coding101.xyz\/index.php?rest_route=\/wp\/v2\/posts\/171\/revisions\/173"}],"wp:attachment":[{"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coding101.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}