{"id":58916,"date":"2023-10-04T08:30:39","date_gmt":"2023-10-04T03:00:39","guid":{"rendered":"https:\/\/2thenew.xyz\/blog\/?p=58916"},"modified":"2024-01-02T17:37:04","modified_gmt":"2024-01-02T12:07:04","slug":"custom-exceptional-handling-using-controlleradvice","status":"publish","type":"post","link":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/","title":{"rendered":"Custom Exceptional Handling using ControllerAdvice"},"content":{"rendered":"<p id=\"76e5\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" data-selectable-paragraph=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"bm abu abv c alignleft\" role=\"presentation\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1AYhaa0lF5Sxfj7R3ukf9qA.png\" alt=\"Blog image\" width=\"700\" height=\"308\" \/><\/p>\n<p class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response.<\/p>\n<h2 id=\"ffe7\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\"><strong class=\"nr gk\">Let\u2019s understand why we need Custom Exceptional Handing<\/strong><\/h2>\n<p id=\"5099\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">Using <code class=\"cn acq acr acs act b\">@ControllerAdvice<\/code> in Spring allows you to create a centralized exception handler that can handle exceptions thrown by controllers in your application. This is a powerful feature for custom exception handling. Here&#8217;s a step-by-step guide on how to implement custom exception handling using <code class=\"cn acq acr acs act b\">@ControllerAdvice<\/code> in Spring:<\/p>\n<h3 id=\"e746\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\"><strong class=\"nr gk\">Step 1: Create Custom Exception Classes<\/strong><\/h3>\n<p id=\"410f\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">Create custom exception classes that extend <code class=\"cn acq acr acs act b\">RuntimeException<\/code> Or a suitable base exception class. For example:<\/p>\n<pre class=\"acu acv acw acx acy acz act ada lr adb bh bp\"><span id=\"c8fe\" class=\"rc adc wu act b bl add ade s adf adg\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">ProductNotFoundException<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title.class\">RuntimeException<\/span> {\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title.function\">ProductNotFoundException<\/span><span class=\"hljs-params\">(String message)<\/span> {\r\n        <span class=\"hljs-built_in\">super<\/span>(message);\r\n    }\r\n}<\/span><\/pre>\n<pre class=\"sl acz act ada lr adb bh bp\"><span id=\"382f\" class=\"rc adc wu act b bl add ade s adf adg\" data-selectable-paragraph=\"\"><span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">InvalidRequestException<\/span> <span class=\"hljs-keyword\">extends<\/span> <span class=\"hljs-title.class\">RuntimeException<\/span> {\r\n    <span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-title.function\">InvalidRequestException<\/span><span class=\"hljs-params\">(String message)<\/span> {\r\n        <span class=\"hljs-built_in\">super<\/span>(message);\r\n    }\r\n}<\/span><\/pre>\n<p id=\"9f2b\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">You can create as many custom exception classes as needed for different error scenarios.<\/p>\n<h3 id=\"6fdb\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\"><strong class=\"nr gk\">Step 2: Create a Global Exception Handler<\/strong><\/h3>\n<p id=\"2496\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">Create a global exception handler class annotated with <code class=\"cn acq acr acs act b\">@ControllerAdvice<\/code>. This class will contain methods to handle various exceptions.<\/p>\n<p class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">Here&#8217;s an example:<\/p>\n<pre class=\"acu acv acw acx acy acz act ada lr adb bh bp\"><span id=\"27e0\" class=\"rc adc wu act b bl add ade s adf adg\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@ControllerAdvice<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">GlobalExceptionHandler<\/span> {\r\n\r\n    <span class=\"hljs-meta\">@ExceptionHandler(ProductNotFoundException.class)<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> ResponseEntity&lt;String&gt; <span class=\"hljs-title.function\">handleProductNotFoundException<\/span><span class=\"hljs-params\">(ProductNotFoundException ex)<\/span> {\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">ResponseEntity<\/span>&lt;&gt;(ex.getMessage(), HttpStatus.NOT_FOUND);\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@ExceptionHandler(InvalidRequestException.class)<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> ResponseEntity&lt;String&gt; <span class=\"hljs-title.function\">handleInvalidRequestException<\/span><span class=\"hljs-params\">(InvalidRequestException ex)<\/span> {\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">ResponseEntity<\/span>&lt;&gt;(ex.getMessage(), HttpStatus.BAD_REQUEST);\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@ExceptionHandler(Exception.class)<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> ResponseEntity&lt;String&gt; <span class=\"hljs-title.function\">handleOtherExceptions<\/span><span class=\"hljs-params\">(Exception ex)<\/span> {\r\n        <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">ResponseEntity<\/span>&lt;&gt;(<span class=\"hljs-string\">\"An error occurred: \"<\/span> + ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);\r\n    }\r\n}<\/span><\/pre>\n<p id=\"29c0\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">In this example, we\u2019ve created three exception-handling methods: one for handling <code class=\"cn acq acr acs act b\">ProductNotFoundException<\/code>, one for handling <code class=\"cn acq acr acs act b\">InvalidRequestException<\/code>, and another for handling general exceptions. These methods return appropriate HTTP response statuses and error messages.<\/p>\n<h3 id=\"831a\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\"><strong class=\"nr gk\">Step 3: Throw Custom Exceptions in Your Controllers<\/strong><\/h3>\n<p id=\"7cda\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">In your controller methods, throw custom exceptions when needed. For example:<\/p>\n<pre class=\"acu acv acw acx acy acz act ada lr adb bh bp\"><span id=\"9e56\" class=\"rc adc wu act b bl add ade s adf adg\" data-selectable-paragraph=\"\"><span class=\"hljs-meta\">@RestController<\/span>\r\n<span class=\"hljs-meta\">@RequestMapping(\"\/api\/products\")<\/span>\r\n<span class=\"hljs-keyword\">public<\/span> <span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title.class\">ProductController<\/span> {\r\n    <span class=\"hljs-meta\">@Autowired<\/span>\r\n    <span class=\"hljs-keyword\">private<\/span> ProductService productService;\r\n\r\n    <span class=\"hljs-meta\">@GetMapping(\"\/{id}\")<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> ResponseEntity&lt;Product&gt; <span class=\"hljs-title.function\">getProductById<\/span><span class=\"hljs-params\">(<span class=\"hljs-meta\">@PathVariable<\/span> Long id)<\/span> {\r\n        <span class=\"hljs-type\">Product<\/span> <span class=\"hljs-variable\">product<\/span> <span class=\"hljs-operator\">=<\/span> productService.getProductById(id);\r\n        <span class=\"hljs-keyword\">if<\/span> (product == <span class=\"hljs-literal\">null<\/span>) {\r\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">ProductNotFoundException<\/span>(<span class=\"hljs-string\">\"Product not found with ID: \"<\/span> + id);\r\n        }\r\n        <span class=\"hljs-keyword\">return<\/span> ResponseEntity.ok(product);\r\n    }\r\n\r\n    <span class=\"hljs-meta\">@PostMapping<\/span>\r\n    <span class=\"hljs-keyword\">public<\/span> ResponseEntity&lt;String&gt; <span class=\"hljs-title.function\">createProduct<\/span><span class=\"hljs-params\">(<span class=\"hljs-meta\">@RequestBody<\/span> Product product)<\/span> {\r\n        <span class=\"hljs-keyword\">if<\/span> (product == <span class=\"hljs-literal\">null<\/span>) {\r\n            <span class=\"hljs-keyword\">throw<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title.class\">InvalidRequestException<\/span>(<span class=\"hljs-string\">\"Invalid product request\"<\/span>);\r\n        }\r\n        productService.createProduct(product);\r\n        <span class=\"hljs-keyword\">return<\/span> ResponseEntity.status(HttpStatus.CREATED).body(<span class=\"hljs-string\">\"Product created successfully\"<\/span>);\r\n    }\r\n    <span class=\"hljs-comment\">\/\/ Other controller methods<\/span>\r\n}<\/span><\/pre>\n<p id=\"35a4\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">In this example, when a product is not found (<code class=\"cn acq acr acs act b\">getProductById<\/code>) or an invalid request is made (<code class=\"cn acq acr acs act b\">createProduct<\/code>), we throw custom exceptions (<code class=\"cn acq acr acs act b\">ProductNotFoundException<\/code> and <code class=\"cn acq acr acs act b\">InvalidRequestException<\/code>).<\/p>\n<h3 id=\"af06\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\"><strong class=\"nr gk\">Step 4: Test the Exception Handling<\/strong><\/h3>\n<p id=\"91b3\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">Now, when you access your RESTful endpoints and an exception is thrown, the global exception handler will catch the exception and return the appropriate HTTP status and error message.<\/p>\n<p id=\"da21\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">For example, if you make a GET request to <code class=\"cn acq acr acs act b\">\/api\/products\/123<\/code>, and a product with ID 123 is not found, the response will have a status code of 404 (NOT FOUND) and an error message like &#8220;Product not found with ID: 123.&#8221;<\/p>\n<p id=\"e420\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">By following these steps, you\u2019ve added custom exception handling using <code class=\"cn acq acr acs act b\">@ControllerAdvice<\/code> in your Spring Boot application, allowing you to handle specific exceptions gracefully and provide meaningful error responses to clients.<\/p>\n<figure class=\"acu acv acw acx acy abp lz ma paragraph-image\">\n<div class=\"abq abr by abs bm abt\" role=\"button\">\n<div class=\"lz ma abj\"><img loading=\"lazy\" decoding=\"async\" class=\"bm abu abv c\" role=\"presentation\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1bnnVpE_tWprLZ2WsThxDQA.png\" alt=\"Blog image\" width=\"700\" height=\"308\" \/><\/div>\n<\/div>\n<\/figure>\n<p id=\"67a0\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">After we fix this issue, we&#8217;ll be getting a proper response. You can have a look at the code here on <a class=\"ay hj\" href=\"https:\/\/github.com\/themoinmalik\/ExceptionHandling\" target=\"_blank\" rel=\"noopener ugc nofollow\">github<\/a>.<\/p>\n<p id=\"5260\" class=\"pw-post-body-paragraph abw abx wu nr b aby abz aca acb acc acd ace acf nb acg ach aci ng acj ack acl nl acm acn aco acp kc bp\" style=\"text-align: left;\" data-selectable-paragraph=\"\">You can comment down below or email me your queries.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle [&hellip;]<\/p>\n","protected":false},"author":1507,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":37,"footnotes":""},"categories":[446,1994,3109],"tags":[5489],"class_list":["post-58916","post","type-post","status-publish","format-standard","hentry","category-java","category-software-development","category-user-experience","tag-exceptionalhandling"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Moin Malik\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#article\",\"name\":\"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog\",\"headline\":\"Custom Exceptional Handling using ControllerAdvice\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/moin-malik\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"image\":{\"@type\":\"ImageObject\",\"url\":\"\\\/blog\\\/wp-ttn-blog\\\/uploads\\\/2024\\\/01\\\/1AYhaa0lF5Sxfj7R3ukf9qA.png\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#articleImage\"},\"datePublished\":\"2023-10-04T08:30:39+05:30\",\"dateModified\":\"2024-01-02T17:37:04+05:30\",\"inLanguage\":\"en-US\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#webpage\"},\"articleSection\":\"Java\\\/JVM, Software development, User Experience, exceptionalhandling\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/java\\\/#listItem\",\"name\":\"Java\\\/JVM\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/java\\\/#listItem\",\"position\":2,\"name\":\"Java\\\/JVM\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/java\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#listItem\",\"name\":\"Custom Exceptional Handling using ControllerAdvice\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#listItem\",\"position\":3,\"name\":\"Custom Exceptional Handling using ControllerAdvice\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/java\\\/#listItem\",\"name\":\"Java\\\/JVM\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/moin-malik\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/moin-malik\\\/\",\"name\":\"Moin Malik\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#authorImage\",\"url\":\"https:\\\/\\\/newersworld-sf-static.tothenew.net\\\/prod\\\/profilePicFolder\\\/e0b65435-8683-4a84-b7d3-0eee31bb71ca_4771-Moin-Malik-PROFILEPICTURE.jpeg\",\"width\":96,\"height\":96,\"caption\":\"Moin Malik\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/\",\"name\":\"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog\",\"description\":\"Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/custom-exceptional-handling-using-controlleradvice\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/moin-malik\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/moin-malik\\\/#author\"},\"datePublished\":\"2023-10-04T08:30:39+05:30\",\"dateModified\":\"2024-01-02T17:37:04+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog","description":"Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle","canonical_url":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#article","name":"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog","headline":"Custom Exceptional Handling using ControllerAdvice","author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/moin-malik\/#author"},"publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"},"image":{"@type":"ImageObject","url":"\/blog\/wp-ttn-blog\/uploads\/2024\/01\/1AYhaa0lF5Sxfj7R3ukf9qA.png","@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#articleImage"},"datePublished":"2023-10-04T08:30:39+05:30","dateModified":"2024-01-02T17:37:04+05:30","inLanguage":"en-US","mainEntityOfPage":{"@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#webpage"},"articleSection":"Java\/JVM, Software development, User Experience, exceptionalhandling"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.xyz\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/java\/#listItem","name":"Java\/JVM"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/java\/#listItem","position":2,"name":"Java\/JVM","item":"https:\/\/2thenew.xyz\/blog\/category\/java\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#listItem","name":"Custom Exceptional Handling using ControllerAdvice"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#listItem","position":3,"name":"Custom Exceptional Handling using ControllerAdvice","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.xyz\/blog\/category\/java\/#listItem","name":"Java\/JVM"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.xyz\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.xyz\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.xyz\/blog\/author\/moin-malik\/#author","url":"https:\/\/2thenew.xyz\/blog\/author\/moin-malik\/","name":"Moin Malik","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#authorImage","url":"https:\/\/newersworld-sf-static.tothenew.net\/prod\/profilePicFolder\/e0b65435-8683-4a84-b7d3-0eee31bb71ca_4771-Moin-Malik-PROFILEPICTURE.jpeg","width":96,"height":96,"caption":"Moin Malik"}},{"@type":"WebPage","@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#webpage","url":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/","name":"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog","description":"Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.xyz\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/moin-malik\/#author"},"creator":{"@id":"https:\/\/2thenew.xyz\/blog\/author\/moin-malik\/#author"},"datePublished":"2023-10-04T08:30:39+05:30","dateModified":"2024-01-02T17:37:04+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.xyz\/blog\/#website","url":"https:\/\/2thenew.xyz\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.xyz\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog","og:description":"Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle","og:url":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/","og:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Custom Exceptional Handling using ControllerAdvice | TO THE NEW Blog","twitter:description":"Here, we can see in the above image that the product is not present for this product ID. But we are getting proper 200 status. which is not a proper understandable response. Let\u2019s understand why we need Custom Exceptional Handing Using @ControllerAdvice in Spring allows you to create a centralized exception handler that can handle","twitter:image":"https:\/\/2thenew.xyz\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"58916","title":null,"description":null,"keywords":[],"keyphrases":{"focus":[],"additional":[]},"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":"","og_custom_url":null,"og_article_section":null,"og_article_tags":[],"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":{"id":"#aioseo-article-65e065a4a61a1","slug":"article","graphName":"Article","label":"Article","properties":{"type":"BlogPosting","name":"#post_title","headline":"#post_title","description":"#post_excerpt","image":"","keywords":"","author":{"name":"#author_name","url":"#author_url"},"dates":{"include":true,"datePublished":"","dateModified":""}}},"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":"default","schema_type_options":"{\"article\":{\"articleType\":\"BlogPosting\"},\"course\":{\"name\":\"\",\"description\":\"\",\"provider\":\"\"},\"faq\":{\"pages\":[]},\"product\":{\"reviews\":[]},\"recipe\":{\"ingredients\":[],\"instructions\":[],\"keywords\":[]},\"software\":{\"reviews\":[],\"operatingSystems\":[]},\"webPage\":{\"webPageType\":\"WebPage\"}}","pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":"-1","robots_max_videopreview":"-1","robots_max_imagepreview":"large","priority":null,"frequency":"default","local_seo":null,"limit_modified_date":false,"created":"2023-10-06 03:00:39","updated":"2024-02-29 11:08:20","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.xyz\/blog\/category\/java\/\" title=\"Java\/JVM\">Java\/JVM<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCustom Exceptional Handling using ControllerAdvice\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.xyz\/blog"},{"label":"Java\/JVM","link":"https:\/\/2thenew.xyz\/blog\/category\/java\/"},{"label":"Custom Exceptional Handling using ControllerAdvice","link":"https:\/\/2thenew.xyz\/blog\/custom-exceptional-handling-using-controlleradvice\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/58916","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/users\/1507"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/comments?post=58916"}],"version-history":[{"count":4,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/58916\/revisions"}],"predecessor-version":[{"id":59656,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/posts\/58916\/revisions\/59656"}],"wp:attachment":[{"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/media?parent=58916"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/categories?post=58916"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.xyz\/blog\/wp-json\/wp\/v2\/tags?post=58916"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}