我正在尝试使用 Tomcat 服务器构建一个简单的基于 Spring 的 Web 应用程序。当我尝试通过创建 URL 端点来测试 Controller 是否正常工作时,在向该端点发送 GET 请求时收到 HTTP 404 错误。
基本 URL 为“http://localhost:8080/assignment2/ ”
我正在尝试在“/trafficameras”上设置端点
Controller 类中的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:";
}
关于java - Controller @RequestMapping URL 返回 404 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58108023/