Skip to content

@ExceptionHandler did not take RequestToViewNameTranslator into resolution process [SPR-7298] #11957

@spring-projects-issues

Description

@spring-projects-issues

Simon Wong opened SPR-7298 and commented

The issue arose when I use @ExceptionHandler and trying to render the view through ContentNegotiatingViewResolver

@ExceptionHandler(Exception.class)
public Map<String, Object> myExceptionHandler(Exception e) {
    Map<String, Object> modelMap = new HashMap<String, Object>();
    modelMap.put("modelKey", new Object());

    return modelMap;
}

I expect that it will pick the best view through media type, but it fails and get the following exception.

javax.servlet.ServletException: ModelAndView [ModelAndView: materialized View is [null]; model is {modelKey=java.lang.Object@1f2a9da}] neither contains a view name nor a View object in servlet with name 'dispatcher'
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1054)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:801)

After some code tracing, I found that the AnnotationMethodHandlerExceptionResolver.getModelAndView() will return a new ModelAndView object.

...
else if (returnValue instanceof Map) {
return new ModelAndView().addAllObjects((Map) returnValue);
}
...

And in DispatcherServlet.doDispatch(), the default view name didn't be set again after the return of processHandlerException(), that construct a new ModelAndView object


Affects: 3.0.3

Referenced from: commits d49c067

1 votes, 2 watchers

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions