0
我有一個與spring-petclinic應用程序非常相似的配置,它看起來像SimpleMappingExceptionResolver
工作正常並解決了視圖「錯誤」,但不能很好地與ContentNegotiatingViewResolver
解決這一觀點:Spring MVC 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)
...
這裏的調試日誌:
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>
如果通過堆棧跟蹤行的末尾讀,你會看到的問題是,它無法連接到mongoDB。 – dimitrisli
是的,我知道。我正在故意測試錯誤處理。 – helmy