【问题标题】:PathVariable does not work at allPathVariable 根本不起作用
【发布时间】:2014-12-15 09:13:09
【问题描述】:

由于某种原因,我的 @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'

我的线索不多了,有人知道发生了什么吗?为什么后者不起作用?

我的相关@​​987654327@:

<?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>

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>

【问题讨论】:

  • 您是否启用了此模块的日志记录,并研究了初始化(报告每个映射的位置)和请求处理(应该报告未找到匹配的控制器方法的位置)?我最近使用了这个功能,没有遇到任何问题。
  • @MarkoTopolnik 它给了我以下信息: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'.
  • 你看到了“映射 URL ... 到控制器方法 ...”或类似的行吗?
  • 是的@MarkoTopolnik 24-Aug-2014 17:31:20.333 INFO [http-apr-8080-exec-40] org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping.registerHandler Mapped URL path [/bot/*] onto handler 'botController',因为http://localhost:8080/GithubHookSEChatService/bot/test 确实有效。不过,我无法在日志中看到这两种方法的确切映射。
  • 我现在有一个有用的线索:@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

标签: java spring-mvc


【解决方案1】:

这个问题似乎与@PathVariable 无关,它实际上也失败了:

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

发生这种情况是因为存在多个级别的子路径,我在In Spring MVC, how can I map nested URLs such as /settings/, /settings/users/, and /settings/users/delete?找到了我的问题的解决方案

您唯一需要做的就是在您的dispatcher-servlet.xml 中包含以下内容:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

然后一切都按预期工作!

【讨论】:

  • 您是否考虑过少用 XML?能够只使用 Java 是一种解脱,包括类型安全和自动导入的好处。
  • @MarkoTopolnik 我已经考虑过了,但到目前为止还没有设法让它与例如配置文件一起工作。
  • 你的意思是 .properties 文件?
  • 仍然,默认情况下使用 AnnotationHandler,因此您必须在某处进行一些非标准配置。我的猜测是您在上下文中只是想念&lt;mvc:annotation-driven /&gt;。另一个答案是错误的,因为它不是 component-scanDefaultAnnotationHandlerMapping 添加到上下文中,因为它几乎没有注意到特定于 mvc 的东西。
【解决方案2】:

我知道这是个老问题。但答案会对其他人有所帮助。

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

&lt;mvc:annotation-driven/&gt;

【讨论】:

    猜你喜欢
    • 2012-01-07
    • 1970-01-01
    • 1970-01-01
    • 2015-08-18
    • 2011-06-25
    • 1970-01-01
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多