Spring MVC无Boot项目404请求映射问题求助
Hey there! Let’s troubleshoot that 404 error you’re hitting with plain Spring (no Boot)—that warning about missing mappings usually boils down to a few common configuration issues. Let’s walk through the fixes step by step:
1. Fix the DispatcherServlet’s alwaysUseFullPath Configuration
The warning mentions your DispatcherServlet is named alwaysUseFullPath, which is a big clue. If this parameter is set to true, the servlet will try to match the full URI (like /UserService/working) against your @RequestMapping paths, instead of stripping off the servlet’s mapped prefix. That’s almost certainly causing the mismatch here.
If using web.xml:
Check your servlet configuration and ensure alwaysUseFullPath is set to false (this is the default, but it’s easy to accidentally flip it):
<servlet> <servlet-name>alwaysUseFullPath</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>alwaysUseFullPath</param-name> <param-value>false</param-value> <!-- Critical: keep this false --> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>alwaysUseFullPath</servlet-name> <url-pattern>/UserService/*</url-pattern> <!-- Maps all /UserService/* requests to this servlet --> </servlet-mapping>
With this setup, when you hit /UserService/working, the DispatcherServlet will pass /working to your controllers to match against @RequestMapping paths.
If using Java-based configuration (WebApplicationInitializer):
Avoid setting setAlwaysUseFullPath(true) on your DispatcherServletRegistrationBean:
public class MyWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebConfig.class); DispatcherServlet dispatcherServlet = new DispatcherServlet(ctx); ServletRegistration.Dynamic registration = servletContext.addServlet("alwaysUseFullPath", dispatcherServlet); registration.setLoadOnStartup(1); registration.addMapping("/UserService/*"); // DO NOT CALL registration.setInitParameter("alwaysUseFullPath", "true"); } }
2. Ensure Your Controller is Scanned by Spring
Spring can’t map your @RequestMapping if it doesn’t know your controller exists. Double-check:
- Your controller class has
@Controlleror@RestControllerannotation:@Controller public class UserServiceController { @RequestMapping("/working") public void working() { System.out.println("working"); } } - Your Spring configuration enables component scanning for the package containing your controller:
- In XML (
applicationContext.xmlor your DispatcherServlet’s context XML):<context:component-scan base-package="com.yourcompany.yourproject.controllers"/> - In Java configuration:
@Configuration @ComponentScan("com.yourcompany.yourproject.controllers") public class WebConfig { ... }
- In XML (
3. Enable Spring MVC Annotation Support
Without enabling annotation-driven MVC, Spring won’t process @RequestMapping annotations. Add this to your configuration:
- In XML:
<mvc:annotation-driven/> - In Java configuration:
@Configuration @EnableWebMvc // This enables annotation processing @ComponentScan("com.yourcompany.yourproject") public class WebConfig implements WebMvcConfigurer { ... }
4. Verify Your Request Path Mapping
Make sure your controller’s @RequestMapping aligns with the expected URL:
- If your DispatcherServlet is mapped to
/UserService/*, your method’s@RequestMapping("/working")will correctly match/UserService/working. - If you added a class-level
@RequestMapping, adjust accordingly:@Controller @RequestMapping("/UserService") // Class-level prefix public class UserServiceController { @RequestMapping("/working") // Method-level path public void working() { System.out.println("working"); } }
5. Confirm Your Project’s Context Path
Double-check that your project is deployed with the correct context path. For example, if you deployed a WAR named UserService.war to Tomcat, the context path will be /UserService (which matches your URL). If the context path is different (e.g., /MyApp), your URL would need to be http://localhost:8081/MyApp/UserService/working—Tomcat’s startup logs will show the deployed context path if you’re unsure.
Start with checking the alwaysUseFullPath parameter first—it’s the most likely culprit given the warning message you shared. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Robertas Ankudovicius

