PathVariable完全不起作用

5

出于某种原因,我的@PathVariable注释完全不起作用,在进行一些谷歌搜索后,我没有找到其他有相同问题的人,这是代码:

@Controller
@RequestMapping("/bot")
public class BotController {
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public void test() {
        System.out.println("test");
        Store.INSTANCE.getChatBot().postMessage("test");
    }

    @RequestMapping(value = "/say/{text}", method = RequestMethod.GET)
    public void say(final @PathVariable("text") String text) {
        System.out.println("say: " + text);
        Store.INSTANCE.getChatBot().postMessage(text);
    }
}

这个可以用: http://localhost:8080/GithubHookSEChatService/bot/test

这个不行: http://localhost:8080/GithubHookSEChatService/bot/say/realtest

除了System.out.println("say: " + text)没有执行外,我能提供的唯一线索是:

24-Aug-2014 17:25:21.611 WARNING [http-apr-8080-exec-24] 
org.springframework.web.servlet.PageNotFound.noHandlerFound 
No mapping found for HTTP request with URI 
[/GithubHookSEChatService/bot/say/realtest] in DispatcherServlet with name 'dispatcher'

我无法找到线索,有人知道发生了什么吗?为什么后者不起作用?

我的相关web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" 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">
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
</web-app>

and

dispatcher-servlet.xml:

<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" xmlns:context="http://www.springframework.org/schema/context">

    <!-- auto scan -->
    <context:component-scan base-package="com.skiwi.githubhooksechatservice" />

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:githubhooksechatservice-environment.properties</value>
        </property>
    </bean>

    <!-- properties -->
    <bean id="configuration" class="com.skiwi.githubhooksechatservice.mvc.configuration.Configuration">
        <property name="rootUrl" value="${env.rootUrl}"/>
        <property name="chatUrl" value="${env.chatUrl}"/>
        <property name="botEmail" value="${env.botEmail}"/>
        <property name="botPassword" value="${env.botPassword}"/>
        <property name="roomId" value="${env.roomId}"/>
    </bean>

    <!-- startup bean -->
    <bean name="startup" init-method="start" class="com.skiwi.githubhooksechatservice.mvc.beans.StartupBean" lazy-init="false" />
</beans>

是的@MarkoTopolnik24-Aug-2014 17:31:20.333 INFO [http-apr-8080-exec-40] org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping.registerHandler 将URL路径 [/bot/*] 映射到处理程序 'botController', 因为 http://localhost:8080/GithubHookSEChatService/bot/test 可以工作。不过,在日志中我无法看到两个方法的确切映射关系。 - skiwi
嗯...我会加上@ResponseBodyvoid返回类型的前面。 - Marko Topolnik
那个输出对我来说有点可疑...我总是看到每个方法都有特定的映射。这可能确实表明没有实际映射任何内容。请尝试我的上述建议。 - Marko Topolnik
@ResponseBody 对于查看请求是否成功非常有帮助 :) 在 test 上我得到了一个空白页面,在 say 上是 404。 - skiwi
1
我现在有一个有用的线索:@RequestMapping(value = "/{text}", method = RequestMethod.GET) 可以与 http://localhost:8080/GithubHookSEChatService/bot/realtest 一起使用,但是 @RequestMapping(value = "/say/{text}", method = RequestMethod.GET) 仍然不能与 http://localhost:8080/GithubHookSEChatService/bot/say/realtest 一起使用。 - skiwi
显示剩余17条评论
2个回答

3
这个问题似乎不仅与@PathVariable有关,实际上它也在以下情况下失败:
@RequestMapping(value = "/test/test", method = RequestMethod.GET)
@ResponseBody
public void test() {
    System.out.println("test");
    Store.INSTANCE.getChatBot().postMessage("test");
}

这是因为存在多个子路径级别,我在Spring MVC中如何映射嵌套URL(例如/settings/、/settings/users/和/settings/users/delete)中找到了解决方案。
您只需要在dispatcher-servlet.xml中添加以下内容即可:
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

然后一切都按预期工作!


你有没有考虑过不使用XML?只使用Java工作会让你感到轻松,包括类型安全和自动导入的好处。 - Marko Topolnik
@MarkoTopolnik 我考虑过,但是到目前为止还没有成功地使用配置文件等方法。 - skiwi
你是指 .properties 文件吗? - Marko Topolnik
仍然,AnnotationHandler 是默认使用的,所以你必须在某个非标准配置中使用它。我猜你只是在上下文中忘记了 <mvc:annotation-driven />。那个答案是错误的,因为它并不是 component-scanDefaultAnnotationHandlerMapping 添加到上下文中,而它对 MVC 特定的事情几乎视而不见。 - soulcheck

0

我知道这是一个老问题。但答案对于其他人也将是有帮助的。

(1) 假设dispatcher-servlet.xml是您在web.xml中的DispatcherServlet名称,请添加以下行:

<mvc:annotation-driven/>


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接