简体   繁体   English

为什么Spring DispatcherServlet找不到我的控制器方法?

[英]Why isn't the Spring DispatcherServlet finding my controller method?

Let's say I have the following Spring controller: 假设我有以下Spring控制器:

@Controller
public class FooController
{
    @RequestMapping(value = "/foo", method = RequestMethod.GET, headers = { "Content-Type=text/plain" })
    @ResponseBody
    public String foo(String bar)
    {
        return "Bar is " + bar;
    }
}

Here's the associated web.xml file: 这是关联的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns="http://java.sun.com/xml/ns/javaee"
     xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     id="WebApp_ID"
     version="2.5">
  <display-name>Foo</display-name>
  <servlet>
    <servlet-name>FooServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/servlet.xml</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>FooServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>
</web-app>

And here's the associated servlet.xml file: 这是关联的servlet.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.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
  <mvc:annotation-driven/>
  <bean id="FooController" class="foo.FooController"/>
</beans>

FYI, I'm using version 3.0.5 of Spring Framework. 仅供参考,我使用的是Spring Framework版本3.0.5。 I know this is an old version, but it's a constraint that I have to work under. 我知道这是一个旧版本,但这是我必须遵循的约束。

So I deploy the WAR file (eg FooApp.war) built from the above to Tomcat (version 7.0.67). 因此,我将从上面构建的WAR文件(例如FooApp.war)部署到Tomcat(7.0.67版)中。 Everything starts up fine. 一切开始正常。 But when I navigate to http://localhost:8080/FooApp/foo?bar=baz , I get a 404 error in the browser and the following in the Tomcat console: 但是,当我导航到http:// localhost:8080 / FooApp / foo?bar = baz时 ,我在浏览器中收到404错误,在Tomcat控制台中收到以下错误:

Apr 28, 2016 9:43:33 AM org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver handleNoSuchRequestHandlingMethod
WARNING: No matching handler method found for servlet request: path '/foo', method 'GET', parameters map['bar' -> array<String>['baz']]

So my question is, what am I doing wrong in my configuration that's causing this issue? 所以我的问题是,导致该问题的配置在做错什么? I'm sure it's something, but I'm having trouble figuring out what it is. 我确定它是某种东西,但是我很难弄清它是什么。

Edit: Removed the SimpleUrlHandlerMapping , per M. Deinum's comment. 编辑: SimpleUrlHandlerMapping M. Deinum的注释,删除了SimpleUrlHandlerMapping

I downloaded the example project from here and started looking at what was different between it and my test project. 我从这里下载示例项目,然后开始查看它与测试项目之间的区别。 I finally narrowed it down to the presence of headers = { "Content-Type=text/plain" } in the @RequestMapping annotation in my controller method. 我最终将其范围缩小到控制器方法中@RequestMapping批注中的headers = { "Content-Type=text/plain" } After removing it, everything works. 删除后,一切正常。

Add "context:component-scan base-package="package name" /" to servlet.xml. 将“ context:component-scan base-package =”软件包名称“ /”添加到servlet.xml。 This will enable auto scanning of your controller. 这将启用对控制器的自动扫描。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM