@ControllerAdvice private static class ModelAttributeAdvice { @SuppressWarnings("unused") @ModelAttribute public void addAttributes(Model model) { model.addAttribute("attr1", "gAttr1"); model.addAttribute("attr2", "gAttr2"); } }
@ControllerAdvice("java.lang") private static class ModelAttributeNotUsedPackageAdvice { @SuppressWarnings("unused") @ModelAttribute public void addAttributes(Model model) { model.addAttribute("attr3", "gAttr3"); } }
@ControllerAdvice private static class MyControllerAdvice { @ExceptionHandler public ResponseEntity<String> handle(IllegalStateException ex) { return ResponseEntity.status(400).body("Handled exception"); } }
@ControllerAdvice({"org.springframework.web.servlet.mvc.method.annotation", "java.lang"}) private static class ModelAttributePackageAdvice { @SuppressWarnings("unused") @ModelAttribute public void addAttributes(Model model) { model.addAttribute("attr2", "gAttr2"); } }
@ControllerAdvice private static class GlobalExceptionHandler { @ExceptionHandler public String handleException(IllegalStateException exception) { return "globalErrorView"; } }
@ControllerAdvice private static class ApplicationExceptionHandler extends ResponseEntityExceptionHandler { @Override protected ResponseEntity<Object> handleServletRequestBindingException( ServletRequestBindingException ex, HttpHeaders headers, HttpStatus status, WebRequest request) { headers.set("someHeader", "someHeaderValue"); return handleExceptionInternal(ex, "error content", headers, status, request); } }
@ControllerAdvice static class TestControllerAdvice { @InitBinder void initDataBinder() {} @ModelAttribute void initModel() {} @ExceptionHandler void handleException(IllegalStateException ex) {} }
@ControllerAdvice private static class TestControllerAdvice { private boolean exceptionHandled; public boolean isExceptionHandled() { return this.exceptionHandled; } @MessageExceptionHandler public void handleException(IllegalStateException ex) { this.exceptionHandled = true; } }
@ControllerAdvice private static class ModelAttributeAdvice { @ModelAttribute("globalAttrName") public String getAttribute() { return "Global Attribute Value"; } }
@ControllerAdvice(annotations = Controller.class) private static class TargetedControllerAdvice implements ResponseBodyAdvice<String> { @Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { return true; } @Override public String beforeBodyWrite(String body, MethodParameter returnType, MediaType contentType, Class<? extends HttpMessageConverter<?>> converterType, ServerHttpRequest request, ServerHttpResponse response) { return body + "-TargetedControllerAdvice"; } }
@ControllerAdvice private static class MyControllerAdvice implements ResponseBodyAdvice<String> { @Override public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) { return true; } @Override public String beforeBodyWrite(String body, MethodParameter returnType, MediaType contentType, Class<? extends HttpMessageConverter<?>> converterType, ServerHttpRequest request, ServerHttpResponse response) { return body + "-MyControllerAdvice"; } }
@ControllerAdvice private static class ResponseCodeSuppressingAdvice extends AbstractMappingJacksonResponseBodyAdvice { @Override protected void beforeBodyWriteInternal(MappingJacksonValue bodyContainer, MediaType contentType, MethodParameter returnType, ServerHttpRequest request, ServerHttpResponse response) { int status = ((ServletServerHttpResponse) response).getServletResponse().getStatus(); response.setStatusCode(HttpStatus.OK); Map<String, Object> map = new LinkedHashMap<>(); map.put("status", status); map.put("message", bodyContainer.getValue()); bodyContainer.setValue(map); } }
@ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class}) static class AssignableTypesSupport {}
@ControllerAdvice(annotations = ControllerAnnotation.class) static class AnnotationSupport {}
@ControllerAdvice(basePackageClasses = MarkerClass.class) static class MarkerClassSupport {}
@ControllerAdvice(basePackages = "org.springframework.web.method") static class BasePackageSupport {}
@ControllerAdvice("org.springframework.web.method") static class BasePackageValueSupport {}
@ControllerAdvice(annotations = ControllerAnnotation.class, assignableTypes = ControllerInterface.class) static class MultipleSelectorsSupport {}
@ControllerAdvice static class SimpleControllerAdvice {}
@ControllerAdvice @Order(2) static class SecondControllerAdvice { @ExceptionHandler({IllegalStateException.class, IllegalAccessException.class}) public String handleException(Exception ex) { return "SecondControllerAdvice: " + ClassUtils.getShortName(ex.getClass()); } }