Sitemap
Full Stack Developer

Full Stack Developer

This blog is curated just for you, if you are a Developer planning to appear for interviews OR looking out for new & interesting things happening in Java Space.

@ControllerAdvice Tutorial with GitHub project Link

2 min readOct 12, 2024

--

Simplest ControllerAdvice example you have seen! Easy to learn.

Press enter or click to view image in full size
Photo by James Butterly on Unsplash

What is @ControllerAdvice?

@ControllerAdvice is a specialized annotation in the Spring Framework that enables centralized exception handling, model binding, and data pre-processing/post-processing across multiple controllers in a Spring MVC application. Instead of handling exceptions or data transformations in each controller individually, @ControllerAdvice provides a mechanism to apply global logic to all or specific controllers.

Who Made It?

@ControllerAdvice is part of the Spring Framework, developed by Pivotal Software (now part of VMware). It was introduced in Spring 3.2 as a way to simplify handling exceptions and common concerns across controllers.

Package

@ControllerAdvice belongs to the org.springframework.web.bind.annotation package.

import org.springframework.web.bind.annotation.ControllerAdvice;

What Kinds of Projects Can Use It?

@ControllerAdvice is widely applicable in Spring MVC or Spring Boot web projects. It can be used in various kinds of projects, including:

REST API Projects:

To handle exceptions globally, manage API error responses, and bind data to requests or responses for consistent behavior across all endpoints.

Get Full Stack Developer’s stories in your inbox

Join Medium for free to get updates from this writer.

Web Applications:

Useful for handling model attributes, form validation, or binding data across web pages with @ModelAttribute and @InitBinder.

Microservices:

In microservices architectures, it helps standardize exception handling and response formats across multiple REST APIs.

Enterprise Applications:

Enterprise-level projects use @ControllerAdvice to manage cross-cutting concerns like logging, security validation, or error handling in a scalable and centralized manner.

E-Commerce Applications:

Projects that need consistent validation, exception handling, and model binding across different controllers handling customer orders, inventory, and payment services can benefit from @ControllerAdvice.

In general, any Spring MVC or Spring Boot project that requires structured exception handling or global controller-related concerns can make effective use of @ControllerAdvice.

Here is a link to GITHUB controller-advice-demo. It consists of 4 classes only.

  • Global Exception Handling: Centralized handling of exceptions across all controllers. (GlobalExceptionHandler.java)
  • Model Attribute Binding: Shared model attributes available to all controllers. (Response.java)
  • Custom Error Responses: Standardized error response format for API clients. (CustomResourceNotFound.java)
  • Pre-Processing and Post-Processing: Logic that can be applied before and after controller methods. (@ControllerAdvice)

--

--

Written by Full Stack Developer

This blog is curated just for you, if you are a Developer planning to appear for interviews OR looking out for new & interesting things happening in Java Space.