Does it make sense to have an annotated (@Controller) Abstract class in Spring MVC driven container, basically would like to place most of the reusable methods such as exception handlers in the Abstract class and extend that with the base class, so that don’t have to repeat the same boilerplate code. For example.
Abstract Controller Class:
@Controller
abstract class AbstractExternalController {
@ExceptionHandler(NoSuchRequestHandlingMethodException.class)
@ResponseStatus(value = HttpStatus.NOT_FOUND)
public @ResponseBody ResponseModel handleNotFoundException() {
final ResponseModel response = new ErrorModel();
response.setStatus("404");
response.setMessage("Resource Not Found");
return response;
}
...
}
Base Controller Class
@Controller
class ExternalControllerXXX extends AbstractExternalController {
...
}
It is unnecessary to annotate your
AbstractExternalControllerclass with the@Controlleranntation, although, leaving it there will not break anything. Regardless of whether or not you have the@Controllerannotation, you certainly can have the method annotations, and they will work. YourExternalControllerXXXextending it will be added to the application context (because it is annotated with a streotype annotation), and the@ExceptionHandlerand@ResponseStatusannotations will be honored.