{"id":18948,"date":"2013-11-18T10:00:00","date_gmt":"2013-11-18T08:00:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=18948"},"modified":"2013-11-17T22:29:20","modified_gmt":"2013-11-17T20:29:20","slug":"controlleradvice-improvements-in-spring-4","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html","title":{"rendered":"@ControllerAdvice improvements in Spring 4"},"content":{"rendered":"<p>Among many new features in Spring 4 I found @ControllerAdvice improvements. @ControllerAdvice is a specialization of a @Component that is used to define @ExceptionHandler, @InitBinder, and @ModelAttribute methods that apply to all @RequestMapping methods. Prior to Spring 4, @ControllerAdvice assisted all controllers in the same Dispatcher Servlet. With Spring 4 it has changed. As of Spring 4 @ControllerAdvice may be configured to support defined subset of controllers, whereas the default behavior can be still utilized.<\/p>\n<p><a name=\"more\"><\/a><\/p>\n<h2>@ControllerAdvice assisting all controllers<\/h2>\n<p>Let&#8217;s assume we want to create an error handler that will print application errors to the user. Let&#8217;s assume this is a basic Spring MVC application with Thymeleaf as a view engine and we have an ArticleController with the following @RequestMapping method:<\/p>\n<pre class=\" brush:java\">package pl.codeleak.t.articles;\r\n\r\nimport org.springframework.stereotype.Controller;\r\nimport org.springframework.web.bind.annotation.PathVariable;\r\nimport org.springframework.web.bind.annotation.RequestMapping;\r\n\r\n@Controller\r\n@RequestMapping(\"article\")\r\nclass ArticleController {\r\n\r\n    @RequestMapping(\"{articleId}\")\r\n    String getArticle(@PathVariable Long articleId) {\r\n        throw new IllegalArgumentException(\"Getting article problem.\");\r\n    }\r\n}<\/pre>\n<p>Our method throws an imaginary exception, as we can see. Let&#8217;s now create an exception handler using @ControllerAdvice. (this is not only possible method in Spring to deal with exceptions).<\/p>\n<pre class=\" brush:java\">package pl.codeleak.t.support.web.error;\r\n\r\nimport com.google.common.base.Throwables;\r\nimport org.springframework.web.bind.annotation.ControllerAdvice;\r\nimport org.springframework.web.bind.annotation.ExceptionHandler;\r\nimport org.springframework.web.context.request.WebRequest;\r\nimport org.springframework.web.servlet.ModelAndView;\r\n\r\n@ControllerAdvice\r\nclass ExceptionHandlerAdvice {\r\n\r\n @ExceptionHandler(value = Exception.class)\r\n public ModelAndView exception(Exception exception, WebRequest request) {\r\n  ModelAndView modelAndView = new ModelAndView(\"error\/general\");\r\n  modelAndView.addObject(\"errorMessage\", Throwables.getRootCause(exception));\r\n  return modelAndView;\r\n }\r\n}<\/pre>\n<p>The class is not public, as it does not to be. We added @ExceptionHandler method that will handle all types of Exceptions and it will return the &#8220;error\/general&#8221; view:<\/p>\n<pre class=\"brush:xml\">&lt;!DOCTYPE html&gt;\r\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\" xmlns:th=\"http:\/\/www.thymeleaf.org\"&gt;\r\n&lt;head&gt;\r\n    &lt;title&gt;Error page&lt;\/title&gt;\r\n    &lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=UTF-8\"\/&gt;\r\n    &lt;link href=\"..\/..\/..\/resources\/css\/bootstrap.min.css\" rel=\"stylesheet\" media=\"screen\" th:href=\"@{\/resources\/css\/bootstrap.min.css}\"\/&gt;\r\n    &lt;link href=\"..\/..\/..\/resources\/css\/core.css\" rel=\"stylesheet\" media=\"screen\" th:href=\"@{\/resources\/css\/core.css}\"\/&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;div class=\"container\" th:fragment=\"content\"&gt;\r\n    &lt;div th:replace=\"fragments\/alert :: alert (type='danger', message=${errorMessage})\"&gt; &lt;\/div&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<p>To test the solution we can either run the server or (preferably) create a test with Spring MVC Test module. Thanks to the fact that we use Thymeleaf, we can verify the rendered view:<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<pre class=\" brush:java\">@RunWith(SpringJUnit4ClassRunner.class)\r\n@WebAppConfiguration\r\n@ContextConfiguration(classes = {RootConfig.class, WebMvcConfig.class})\r\n@ActiveProfiles(\"test\")\r\npublic class ErrorHandlingIntegrationTest {\r\n\r\n    @Autowired\r\n    private WebApplicationContext wac;\r\n\r\n    private MockMvc mockMvc;\r\n\r\n    @Before\r\n    public void before() {\r\n        this.mockMvc = webAppContextSetup(this.wac).build();\r\n    }\r\n\r\n    @Test\r\n    public void shouldReturnErrorView() throws Exception {\r\n        mockMvc.perform(get(\"\/article\/1\"))\r\n                .andDo(print())\r\n                .andExpect(content().contentType(\"text\/html;charset=ISO-8859-1\"))\r\n                .andExpect(content().string(containsString(\"java.lang.IllegalArgumentException: Getting article problem.\")));\r\n    }\r\n}<\/pre>\n<p>We expect the content type is text\/html and the view contains the HTML fragment with an error message. Not really user friendly, though. But the test is green.<\/p>\n<p>Using the above solution we provide a general mechanism for handling errors of all our controllers. As mentioned earlier, we can do much more with @ControllerAdvice:. E.g:<\/p>\n<pre class=\" brush:java\">@ControllerAdvice\r\nclass Advice {\r\n\r\n    @ModelAttribute\r\n    public void addAttributes(Model model) {\r\n        model.addAttribute(\"attr1\", \"value1\");\r\n        model.addAttribute(\"attr2\", \"value2\");\r\n    }\r\n\r\n    @InitBinder\r\n    public void initBinder(WebDataBinder webDataBinder) {\r\n        webDataBinder.setBindEmptyMultipartFiles(false);\r\n    }\r\n}<\/pre>\n<h2>@ControllerAdvice assisting selected subset of controllers<\/h2>\n<p>As of Spring 4, @ControllerAdvice can be customized through annotations(), basePackageClasses(), basePackages() methods to select a subset of controllers to assist. I will demonstrate a simple case how to utilize this new feature.<\/p>\n<p>Let&#8217;s assume we want to add an API to expose articles via JSON. So we can define a new controller like this:<\/p>\n<pre class=\" brush:java\">@Controller\r\n@RequestMapping(\"\/api\/article\")\r\nclass ArticleApiController {\r\n\r\n    @RequestMapping(value = \"{articleId}\", produces = \"application\/json\")\r\n    @ResponseStatus(value = HttpStatus.OK)\r\n    @ResponseBody\r\n    Article getArticle(@PathVariable Long articleId) {\r\n        throw new IllegalArgumentException(\"[API] Getting article problem.\");\r\n    }\r\n}<\/pre>\n<p>Our controller is not very sophisticated. It returns an Article as a response body, as @ResponseBody annotation indicates. Of course, we want to deal with exceptions. And we don&#8217;t want to return an error as text\/html but as application\/json. Let&#8217;s create a test then:<\/p>\n<pre class=\" brush:java\">@RunWith(SpringJUnit4ClassRunner.class)\r\n@WebAppConfiguration\r\n@ContextConfiguration(classes = {RootConfig.class, WebMvcConfig.class})\r\n@ActiveProfiles(\"test\")\r\npublic class ErrorHandlingIntegrationTest {\r\n\r\n    @Autowired\r\n    private WebApplicationContext wac;\r\n\r\n    private MockMvc mockMvc;\r\n\r\n    @Before\r\n    public void before() {\r\n        this.mockMvc = webAppContextSetup(this.wac).build();\r\n    }\r\n\r\n    @Test\r\n    public void shouldReturnErrorJson() throws Exception {\r\n        mockMvc.perform(get(\"\/api\/article\/1\"))\r\n                .andDo(print())\r\n                .andExpect(status().isInternalServerError())\r\n                .andExpect(content().contentType(\"application\/json\"))\r\n                .andExpect(content().string(containsString(\"{\\\"errorMessage\\\":\\\"[API] Getting article problem.\\\"}\")));\r\n    }\r\n}<\/pre>\n<p>The test is red. What we can do to make it green? We need to make another advice, this time targeting only our Api controller. For that, we will use @ControllerAdvice annotations() selector. In order to do it we need to either create a customer or use existing annotation. We will use @RestController predefined annotation. Controllers annotated with @RestController assume @ResponseBody semantic by default. We may slighlty modify our controller by replacing @Controller with @RestController and removing @ResponseBody from the handler&#8217;s method:<\/p>\n<pre class=\" brush:java\">@RestController\r\n@RequestMapping(\"\/api\/article\")\r\nclass ArticleApiController {\r\n\r\n    @RequestMapping(value = \"{articleId}\", produces = \"application\/json\")\r\n    @ResponseStatus(value = HttpStatus.OK)\r\n    Article getArticle(@PathVariable Long articleId) {\r\n        throw new IllegalArgumentException(\"[API] Getting article problem.\");\r\n    }\r\n}<\/pre>\n<p>We also need to create another advice that will return ApiError (simple POJO):<\/p>\n<pre class=\" brush:java\">@ControllerAdvice(annotations = RestController.class)\r\nclass ApiExceptionHandlerAdvice {\r\n\r\n    \/**\r\n     * Handle exceptions thrown by handlers.\r\n     *\/\r\n    @ExceptionHandler(value = Exception.class)\r\n    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)\r\n    @ResponseBody\r\n    public ApiError exception(Exception exception, WebRequest request) {\r\n        return new ApiError(Throwables.getRootCause(exception).getMessage());\r\n    }\r\n}<\/pre>\n<p>This time when we run our test suite, both tests are green meaning that ExceptionHandlerAdvice assisted &#8220;standard&#8221; ArticleController whereas ApiExceptionHandlerAdvice assisted ArticleApiController.<\/p>\n<h2>Summary<\/h2>\n<p>In the above scenario I demonstrated how easily we can utilize new configuration capabilities of @ControllerAdvice annotation and I hope you like the change as I do.<\/p>\n<h2>References<\/h2>\n<ul>\n<li><a href=\"https:\/\/jira.springsource.org\/browse\/SPR-10222\">SPR-10222<\/a><\/li>\n<li><a href=\"https:\/\/github.com\/bclozel\/spring-framework\/blob\/b66bfc6221e2d752e46ffd682db5c4f9c1ef73ea\/spring-web\/src\/main\/java\/org\/springframework\/web\/bind\/annotation\/ControllerAdvice.java\">@RequestAdvice annotation documentation<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<div style=\"border: 1px solid #D8D8D8; background: #FAFAFA; width: 100%; padding-left: 5px;\"><b><i>Reference: <\/i><\/b><a href=\"http:\/\/blog.codeleak.pl\/2013\/11\/controlleradvice-improvements-in-spring.html\">@ControllerAdvice improvements in Spring 4<\/a> from our <a href=\"http:\/\/www.javacodegeeks.com\/jcg\">JCG partner<\/a> Rafal Borowiec at the <a href=\"http:\/\/blog.codeleak.pl\/\">Codeleak.pl<\/a> blog.<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Among many new features in Spring 4 I found @ControllerAdvice improvements. @ControllerAdvice is a specialization of a @Component that is used to define @ExceptionHandler, @InitBinder, and @ModelAttribute methods that apply to all @RequestMapping methods. Prior to Spring 4, @ControllerAdvice assisted all controllers in the same Dispatcher Servlet. With Spring 4 it has changed. As of &hellip;<\/p>\n","protected":false},"author":516,"featured_media":240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[30],"class_list":["post-18948","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-spring"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>@ControllerAdvice improvements in Spring 4<\/title>\n<meta name=\"description\" content=\"Among many new features in Spring 4 I found @ControllerAdvice improvements. @ControllerAdvice is a specialization of a @Component that is used to define\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"@ControllerAdvice improvements in Spring 4\" \/>\n<meta property=\"og:description\" content=\"Among many new features in Spring 4 I found @ControllerAdvice improvements. @ControllerAdvice is a specialization of a @Component that is used to define\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2013-11-18T08:00:00+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Rafal Borowiec\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/kolorobot\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rafal Borowiec\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html\"},\"author\":{\"name\":\"Rafal Borowiec\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/b1a0b2657d5dd2459806446ac66d2d52\"},\"headline\":\"@ControllerAdvice improvements in Spring 4\",\"datePublished\":\"2013-11-18T08:00:00+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html\"},\"wordCount\":552,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"keywords\":[\"Spring\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html\",\"name\":\"@ControllerAdvice improvements in Spring 4\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"datePublished\":\"2013-11-18T08:00:00+00:00\",\"description\":\"Among many new features in Spring 4 I found @ControllerAdvice improvements. @ControllerAdvice is a specialization of a @Component that is used to define\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"spring-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2013\\\/11\\\/controlleradvice-improvements-in-spring-4.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"@ControllerAdvice improvements in Spring 4\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/b1a0b2657d5dd2459806446ac66d2d52\",\"name\":\"Rafal Borowiec\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e24680b2ba3dfc13759acf6c1f125e54356bc533e0befe953fea365cadcdaffb?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e24680b2ba3dfc13759acf6c1f125e54356bc533e0befe953fea365cadcdaffb?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/e24680b2ba3dfc13759acf6c1f125e54356bc533e0befe953fea365cadcdaffb?s=96&d=mm&r=g\",\"caption\":\"Rafal Borowiec\"},\"description\":\"Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.\",\"sameAs\":[\"http:\\\/\\\/blog.codeleak.pl\\\/\",\"http:\\\/\\\/pl.linkedin.com\\\/in\\\/borowiec\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/kolorobot\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/rafal-borowiec\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"@ControllerAdvice improvements in Spring 4","description":"Among many new features in Spring 4 I found @ControllerAdvice improvements. @ControllerAdvice is a specialization of a @Component that is used to define","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html","og_locale":"en_US","og_type":"article","og_title":"@ControllerAdvice improvements in Spring 4","og_description":"Among many new features in Spring 4 I found @ControllerAdvice improvements. @ControllerAdvice is a specialization of a @Component that is used to define","og_url":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2013-11-18T08:00:00+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","type":"image\/jpeg"}],"author":"Rafal Borowiec","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/kolorobot","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Rafal Borowiec","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html"},"author":{"name":"Rafal Borowiec","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/b1a0b2657d5dd2459806446ac66d2d52"},"headline":"@ControllerAdvice improvements in Spring 4","datePublished":"2013-11-18T08:00:00+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html"},"wordCount":552,"commentCount":1,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","keywords":["Spring"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html","url":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html","name":"@ControllerAdvice improvements in Spring 4","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","datePublished":"2013-11-18T08:00:00+00:00","description":"Among many new features in Spring 4 I found @ControllerAdvice improvements. @ControllerAdvice is a specialization of a @Component that is used to define","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","width":150,"height":150,"caption":"spring-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2013\/11\/controlleradvice-improvements-in-spring-4.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"@ControllerAdvice improvements in Spring 4"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/b1a0b2657d5dd2459806446ac66d2d52","name":"Rafal Borowiec","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/e24680b2ba3dfc13759acf6c1f125e54356bc533e0befe953fea365cadcdaffb?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/e24680b2ba3dfc13759acf6c1f125e54356bc533e0befe953fea365cadcdaffb?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e24680b2ba3dfc13759acf6c1f125e54356bc533e0befe953fea365cadcdaffb?s=96&d=mm&r=g","caption":"Rafal Borowiec"},"description":"Software developer, Team Leader, Agile practitioner, occasional blogger, lecturer. Open Source enthusiast, quality oriented and open-minded.","sameAs":["http:\/\/blog.codeleak.pl\/","http:\/\/pl.linkedin.com\/in\/borowiec\/","https:\/\/x.com\/https:\/\/twitter.com\/kolorobot"],"url":"https:\/\/www.javacodegeeks.com\/author\/rafal-borowiec"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/18948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/516"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=18948"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/18948\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=18948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=18948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=18948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}