You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring应用XML配置后无法访问端点返回404的配置问题咨询

问题根因

配置中有三处错误直接导致接口返回404:

  • DispatcherServlet映射规则错误:web.xml中配置的<url-pattern>为/*,该规则属于全路径匹配,优先级最高,会拦截包括容器内部转发在内的所有请求,破坏Spring MVC的正常请求处理流程。Spring MVC要求DispatcherServlet映射为/,仅接管所有未被其他Servlet匹配的请求,不会干扰容器内部调度。
  • 缺少组件扫描,请求映射未注册:虽然手动在XML中定义了DogsController的Bean,但未配置<context:component-scan>扫描控制器包,类上的@RestController、@RequestMapping、@GetMapping等Web注解无法被Spring识别,请求映射表中不存在/dogs对应的处理方法,请求进入后找不到处理器直接返回404。
  • 上下文分层配置混乱:配置的ContextLoaderListener默认会加载WEB-INF/applicationContext.xml作为根上下文,同时又将同一个配置文件指定给DispatcherServlet加载为Web子上下文,导致Bean重复加载。Spring MVC的请求映射处理器仅会查找当前Web子上下文中的Controller Bean,不会访问父级根上下文,一旦Controller被注册到根上下文,就会出现找不到映射的问题。
修正步骤
  1. 调整web.xml配置
    • 将DispatcherServlet的<url-pattern>从/*修改为/
    • 单Controller简单场景下直接移除ContextLoaderListener配置,避免上下文加载冲突;如果后续要加Service、Dao等分层Bean,再为ContextLoaderListener指定独立的根上下文配置文件,不要和DispatcherServlet的Web层配置混用。
      修正后web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
    <servlet>
        <servlet-name>spring-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
  1. 调整applicationContext.xml配置
    • 添加组件扫描配置,指定扫描控制器所在包,自动识别带Web注解的类注册Bean
    • 删除手动定义DogsController的<bean>标签,组件扫描会自动完成注册
    • 可选添加<mvc:default-servlet-handler/>,将静态资源请求交给容器默认Servlet处理,避免静态资源被拦截
      修正后applicationContext.xml代码:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.example.dogsrestapplication.controller"/>
    
    <mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <mvc:default-servlet-handler/>
</beans>

修改完成后重启容器,访问对应接口即可正常返回JSON数据。

内容的提问来源于stack exchange,提问作者Nicolazz92

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 04:24:17