I have a ControllerAdvice that renders the requestUri to the model, since the thymeleaf upgrade (3.10) removed the #httpServletRequest attribute.
@ControllerAdvice
class UtilityProvider {
@ModelAttribute("requestUri")
fun requestUri(request: HttpServletRequest): String {
return request.requestURI
}
}
that works fine for all requests, except for those views that are rendered from my ExceptionHandler, which is also defined in a ControllerAdvice:
@ControllerAdvice
public class GlobalExceptionHandlerAdvice() {
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
return ModelAndView("error");
}
}
When the Model is returned from inside my exception handler, the requestUri attribute is not added by the other UtlityProvider ControllerAdvice. Is this expected behavior? Any way I can make the ControllerAdvice also apply to the view rendered by the exception handler?
I tried to fix it by adding @Ordered
annotations to make sure the exceptionHandler advice is applied before the utlityProviderAdvice but the annoations don't seem to have an effect, probably because the exception handler can't be ordered like that.
I fixed it for now by adding the respective attribute in the exception handler manually but would like to avoid that:
@ControllerAdvice
public class GlobalExceptionHandlerAdvice() {
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) {
Map<String, Object> model = new HashMap<>();
model.put("requestUri", request.getRequestURI());
return ModelAndView("error", model);
}
}