Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java PathVariable根本不起作用_Java_Spring Mvc - Fatal编程技术网

Java PathVariable根本不起作用

Java PathVariable根本不起作用,java,spring-mvc,Java,Spring Mvc,出于某种原因,我的@PathVariable注释根本不起作用,在进行了一些谷歌搜索后,我一直无法找到其他有相同问题的人,以下是代码: @Controller @RequestMapping("/bot") public class BotController { @RequestMapping(value = "/test", method = RequestMethod.GET) public void test() { System.out.println("

出于某种原因,我的
@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>
<?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>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>

此问题似乎与
@PathVariable
无关,实际上它在以下方面也会失败:

@RequestMapping(value = "/test/test", method = RequestMethod.GET)
@ResponseBody
public void test() {
    System.out.println("test");
    Store.INSTANCE.getChatBot().postMessage("test");
}
这是因为有多个级别的子路径,我在中找到了问题的解决方案

您只需要在
dispatcherservlet.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>
<?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>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>


然后一切都按预期进行

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

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


您是否启用了此模块的日志记录,并研究了初始化(其中报告了每个映射)和请求处理(其中应报告未找到匹配的控制器方法)?我最近使用了此功能,没有遇到任何问题。@MarkoTopolnik它给了我以下信息:
24-Aug-2014 17:25:21.611警告[http-apr-8080-exec-24]org.springframework.web.servlet.PageNotFound.nohandler未找到URI为[/GithubHookSEChatService/bot/say/realtest]的http请求的映射在名为“dispatcher”
的DispatcherServlet中,您看到一行写着“映射URL…到控制器方法…”或类似的内容?Yes@MarkoTopolnik
2014年8月24日17:31:20.333信息[http-apr-8080-exec-40]org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping.registerHandler映射URL路径[/bot/*]作为
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
。您是否考虑过减少XML?能够只使用Java是一种解脱,包括类型安全和自动导入的好处。@MarkoTopolnik我已经考虑过了,但到目前为止还不能使用配置文件。你是说.properties文件吗?仍然,默认情况下使用AnnotationHandler,因此您必须在某个地方使用一些非标准配置。我的猜测是,您只是错过了上下文中的
。另一个答案是错误的,因为它不是
组件扫描
DefaultAnnotationHandlerMapping
添加到上下文中,因为它几乎忽略了mvc特定的内容。