我的配置与spring-petclinic应用程序非常相似,似乎SimpleMappingExceptionResolver
正常工作并解析为视图"错误",但与ContentNegotiatingViewResolver
无法很好地解决该观点:
javax.servlet.ServletException: Could not resolve view with name 'error' in servlet with name 'audit'
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1204)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
...
这是DEBUG日志:
2014-11-13 14:58:24,249 DEBUG org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver - Resolving exception from handler [public void com.foo.AuditGeneratorController.auditMessage(java.io.Writer,javax.servlet.http.HttpServletRequest) throws java.io.IOException]: com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]
2014-11-13 14:58:24,250 DEBUG org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver - Resolving exception from handler [public void com.foo.AuditGeneratorController.auditMessage(java.io.Writer,javax.servlet.http.HttpServletRequest) throws java.io.IOException]: com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]
2014-11-13 14:58:24,250 DEBUG org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolving exception from handler [public void com.foo.AuditGeneratorController.auditMessage(java.io.Writer,javax.servlet.http.HttpServletRequest) throws java.io.IOException]: com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]
2014-11-13 14:58:24,250 DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving exception from handler [public void com.foo.AuditGeneratorController.auditMessage(java.io.Writer,javax.servlet.http.HttpServletRequest) throws java.io.IOException]: com.mongodb.MongoTimeoutException: Timed out after 10000 ms while waiting for a server that matches AnyServerSelector{}. Client view of cluster state is {type=Unknown, servers=[{address=localhost:27017, type=Unknown, state=Connecting, exception={com.mongodb.MongoException$Network: Exception opening the socket}, caused by {java.net.ConnectException: Connection refused}}]
2014-11-13 14:58:24,250 DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Resolving to default view 'error' for exception of type [com.mongodb.MongoTimeoutException]
2014-11-13 14:58:24,250 DEBUG org.springframework.web.servlet.handler.SimpleMappingExceptionResolver - Exposing Exception as model attribute 'exception'
2014-11-13 14:58:24,251 DEBUG org.springframework.web.servlet.DispatcherServlet - Handler execution resulted in exception - forwarding to resolved error view: ModelAndView: reference to view with name 'error'; model is {exception=com.mongodb.MongoTimeoutException: ... (STACK TRACE SNIPPED)
2014-11-13 14:58:24,253 DEBUG org.springframework.web.accept.FixedContentNegotiationStrategy - Requested media types is text/html (based on default MediaType)
2014-11-13 14:58:24,253 DEBUG org.springframework.web.servlet.view.ContentNegotiatingViewResolver - Requested media types are [] based on Accept header types and producible media types [application/json, application/xml])
2014-11-13 14:58:24,255 DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'error'
2014-11-13 14:58:24,255 DEBUG org.springframework.web.servlet.view.ContentNegotiatingViewResolver - No acceptable view found; returning null
2014-11-13 14:58:24,255 DEBUG org.springframework.web.servlet.DispatcherServlet - Could not complete request
javax.servlet.ServletException: Could not resolve view with name 'error' in servlet with name 'audit'
at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1204)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1005)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:952)
这里是视图解析器配置:
<!-- - The ContentNegotiatingViewResolver delegates to the InternalResourceViewResolver
and BeanNameViewResolver, - and uses the requested media type (determined
by the path extension) to pick a matching view. - When the media type is
'text/html', it will delegate to the InternalResourceViewResolver's JstlView,
- otherwise to the BeanNameViewResolver. -->
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="contentNegotiationManager" ref="cnManager" />
<property name="viewResolvers">
<list>
<!-- Default viewClass: JSTL view (JSP with html output) -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- Example: a logical view name of 'vets' is mapped to '/WEB-INF/jsp/vets.jsp' -->
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Used here for 'xml' and 'atom' views -->
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
</list>
</property>
</bean>
<!-- Simple strategy: only path extension is taken into account -->
<bean id="cnManager"
class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="favorPathExtension" value="true" />
<property name="ignoreAcceptHeader" value="true" />
<property name="defaultContentType" value="text/html" />
<property name="mediaTypes">
<map>
<entry key="html" value="text/html" />
<entry key="xml" value="application/xml" />
<entry key="atom" value="application/atom+xml" />
</map>
</property>
</bean>
答案 0 :(得分:0)
从您的评论中可以看出,您对Spring Error Handling感兴趣。请阅读very descriptive documentation on the subject(SimpleMappingExceptionResolver
部分):您所要做的就是使用默认错误视图注册SimpleMappingExceptionResolver:
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<!-- other params -->
<property name="defaultErrorView" value="yourDefaultViewForErrors"/>
</bean>