java - 找不到控制器 @RequestMapping URL 返回 404
问题描述
我正在尝试使用 Tomcat 服务器构建一个简单的基于 Spring 的 Web 应用程序。当我尝试通过创建 URL 端点来测试我的控制器是否正常工作时,我在向该端点发送 GET 请求时收到 HTTP 404 错误。
基本 URL 是“ http://localhost:8080/assignment2/ ”
我正在尝试在“/trafficameras”上设置一个端点
控制器类中的 requestMapping 方法定义为
@ResponseBody
@RequestMapping(value = "/trafficcameras", params = {"ip_comm_status", "camera_status"}, method=RequestMethod.GET)
public String parse(@RequestParam(value="ip_comm_status", required=false) String ip_comm_status_val, @RequestParam(value="camera_status", required=false) String camera_status_val)
{
return "Result is:";
}
当我尝试向“ http://localhost:8080/assignment2/trafficcameras ”发送 GET 请求时,而不是输出“结果是:”,我得到:
HTTP 404: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
我的 web.xml 文件定义为
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Spring Application</display-name>
<servlet>
<servlet-name>springDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/servletContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
bean 在 servletContext.xml 中定义如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<mvc:annotation-driven />
<bean name="cameraController" class="TrafficCamerasController">
<constructor-arg ref="trafficCamerasServiceImpl"></constructor-arg>
</bean>
<bean name="trafficCamerasServiceImpl" class="TrafficCamerasServiceImpl" />
</beans>
关于为什么会发生这种情况的任何想法?几个小时以来,我一直在挠头。
解决方案
你需要这样做。
@ResponseBody
@RequestMapping(value = "/trafficcameras", method=RequestMethod.GET)
public String parse(@RequestParam(value="ip_comm_status", required=false) String ip_comm_status_val, @RequestParam(value="camera_status", required=false) String camera_status_val)
{
return "Result is:";
}
推荐阅读
- mysql - 简单存储过程参数导致未知错误
- typo3 - TYPO3:自定义扩展 - “扩展名称不能为空”错误
- python - 如何将 For 循环数据写入 CSV 文件
- amazon-web-services - Elasticsearch 快照永久保存
- python - 输入正确密码后无法关闭 Tkinter 的弹出窗口
- java - InternalResourceViewResolver 找不到页面
- python - 为什么来自 URL 的参数在 Django 中返回为未定义?
- javascript - 动画完成后如何调用jQuery动画功能?
- javascript - 打字稿开发环境
- reactjs - 未处理的拒绝(TypeError):无法读取未定义的属性“状态”