2

I readed about Spring in Action book.Spring 3.2 brings another option to the table: controller advice. A controller advice is any class that’s annotated with @ControllerAdvice and has one or more of the following kinds of methods: @ExceptionHandler and @InitBinder and @ModelAttribute annotations.
Can you explain help me?

2 Answers2

0

@ControllerAdvice - Specialization of @Component for classes that declare @ExceptionHandler, @InitBinder, or @ModelAttribute methods to be shared across multiple @Controller classes.

For example, I use class annotated with @ControllerAdvice to store multiple exception handlers, below is example code:

@ControllerAdvice
public class ControllerExceptionHandler {

    @ExceptionHandler(RuntimeException.class)
    public ResponseEntity<String> handleRuntimeException(RuntimeException ex){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception ex){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
    }


}

If You want read something about @InitBinder here is a excellent example

Purpose of @InitBinder in Spring

Annotation @ModelAttribute that binds a method parameter or method return value to a named model attribute, exposed to a web view. Supported for controller classes with @RequestMapping methods.

Example

@ModelAttribute("person")
public Person getPerson(){
    return new Person();
}

This annotated method allows you to access the Person object in your view layer as it is automatically added to models by Spring. Welcome

merc-angel
  • 394
  • 1
  • 5
  • 13
0

@ControllerAdvice is an annotation provided by Spring allowing you to write global code that can be applied to a wide range of controllers varying from all controllers to a chosen package or even a specific annotation.

By default, @ControllerAdvice will apply to all classes that use the @Controller annotation(which extends to classes using @RestController). If you wanted this to be more specific, there are a few properties provided that allow this.

To reduce the applicable classes down by package, you simply need to add the name of the package to the annotation. When a package is chosen, it will be enabled for classes inside that package as well as sub-packages. Multiple packages can also be chosen by following the same process but using an array instead of a singular string (all properties in @ControllerAdvice can be singular or multiple).

@ControllerAdvice("my.chosen.package")
@ControllerAdvice(value = "my.chosen.package")
@ControllerAdvice(basePackages = "my.chosen.package")

To enable @ControllerAdvice for all controllers inside the package that the class (or interface) lives in.

@ControllerAdvice(basePackageClasses = MyClass.class)

To apply to specific classes use assignableTypes.

@ControllerAdvice(assignableTypes = MyController.class)

If you want to apply it to controllers with certain annotations The below snippet would only help controllers annotated with @RestController (which it covers by default) but will not include @Controller annotated classes.

@ControllerAdvice(annotations = RestController.class)

Helpful Post for more insight

[Understanding Spring’s @ControllerAdvice]

Source Code

Aniket
  • 303
  • 5
  • 11