Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PathVariable does not work at all

For some reason, my @PathVariable annotation is not working at all, after doing some Google search I've not been able to find anyone else with the same issue, this is the code:

@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);
    }
}

This works: http://localhost:8080/GithubHookSEChatService/bot/test

This does not work: http://localhost:8080/GithubHookSEChatService/bot/say/realtest

Apart from that the System.out.println("say: " + text) does not happen, the only other clue I have is:

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'

I'm running out of clues, does anyone know what is happening? Why doesn't the latter work?

My relevant 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>
like image 893
skiwi Avatar asked Oct 21 '22 02:10

skiwi


1 Answers

This issue appears to not be related to @PathVariable only, it actually also fails on:

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

This happens because there are multiple levels of subpaths, I found the solution of my issue in In Spring MVC, how can I map nested URLs such as /settings/, /settings/users/, and /settings/users/delete?

The only thing you need to do, is to include the following in your dispatcher-servlet.xml:

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

And then everything works as expected!

like image 147
skiwi Avatar answered Oct 23 '22 11:10

skiwi