我正在开发一个简单的 Spring REST Web 服务。根据我所做的研究,可能有两种类型的 404 异常。例如,
@Controller
@RequestMapping("/person")
@Transactional(readOnly=true)
public class PersonController {
@RequestMapping(value="/data", method={RequestMethod.GET,RequestMethod.POST})
@ResponseStatus(value=HttpStatus.OK)
public Person getPerson() {
return service.getPerson();
}
}
类型 1:http://localhost/myws/person/get
将从 Web 服务中抛出 404。
类型 2:http://localhost/myws/idontexist
将从 Web 服务器容器中抛出 404。就我而言,它是tomcat。
为了处理类型 1,我尝试扩展DefaultHandlerExceptionResolver
和覆盖handleNoSuchRequestHandlingMethod
为了处理类型 2,我在我的web.xml
<错误页面> <错误代码>404</错误代码> <location>/WEB-INF/pages/notfound.jsp</location> </错误页面> <错误页面> <exception-type>java.lang.Throwable</exception-type> <location>/WEB-INF/pages/notfound.jsp</location> </错误页面>我的 servlet xml 看起来像,
<context:component-scan base-package="com" /> <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="ignoreAcceptHeader" value="true" /> <property name="order" value="1" /> <property name="contentNegotiationManager"> <bean class="org.springframework.web.accept.ContentNegotiationManager"> <构造函数参数> <bean class="org.springframework.web.accept.ParameterContentNegotiationStrategy"> <构造函数参数> <地图> <entry key="json" value="application/json"/> <entry key="xml" value="application/xml"/> </地图> </constructor-arg> </豆> </constructor-arg> </豆> </属性> <property name="defaultViews"> <列表> <bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /> <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> <构造函数参数> <bean class="org.springframework.oxm.xstream.XStreamMarshaller"> <property name="autodetectAnnotations" value="true"/> </豆> </constructor-arg> </豆> </列表> </属性> </豆> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="order" value="2" /> <属性名称="前缀"> <value>/WEB-INF/pages/</value> </属性> <属性名称="后缀"> <value>.jsp</value> </属性> </豆> <!--处理内部服务器错误--> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"> <property name="order" value="1"/> </豆> <bean class="org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver" > <property name="order" value="2"/> </豆> <!--RestExceptionHandler 扩展 DefaultHandlerExceptionResolver --> <bean class="com.rest.exception.RestExceptionHandler"> <property name="order" value="3"/> </豆> <!-- 数据源和 Daos...-->
当我点击 Type 2 URL 时,我得到以下异常。
WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/myws/WEB-INF/pages/notfound.jsp] in DispatcherServlet with name 'restservlet'
但我的 JSP 存在于上述位置。可能是什么问题呢?