找不到控制器@RequestMapping URL返回404

时间:2019-09-26 00:20:27

标签: java spring

我正在尝试使用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:";
    }

当我尝试将GET请求发送到“ http://localhost:8080/assignment2/trafficcameras”时,而不是输出“ Result is:”, 我得到:

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>

关于为什么发生这种情况的任何想法?我已经挠头好几个小时了。

1 个答案:

答案 0 :(得分:0)

您需要这样做。

final int someTirePressure = Optional.ofNullable(tires.get("Michelin"))
    .map(Tire::getPressure)
    .orElse(defaultTirePressure);