1

I am running simple Spring MVC application. In web.xml, I used following mapping configuration:

<servlet-mapping>
    <servlet-name>spring-web</servlet-name>
    <url-pattern></url-pattern> <!-- root request -->
    <url-pattern>/endpoint/*</url-pattern> <!-- /endpoint/${name} -->
</servlet-mapping>

My controller is defined as:

    @RequestMapping(value = "/endpoint/{name}", method = RequestMethod.GET)
    public ModelAndView render(@PathVariable("name") String name) {
        ModelAndView genericRenderStructure = new ModelAndView();
        genericRenderStructure.setViewName("WEB-INF/views/index.jsp");
        genericRenderStructure.addObject("endpointName", name);

        return genericRenderStructure;
    }

And yet render function is not triggered when visiting the path /endpoints/something. When I change the url-pattern to particular variable name, like <url-pattern>/endpoint/something</url-pattern>, the page starts working on such url. Why doesn't the wildcard symbol match my "whatever" used in url? Is this the problem with RequestMapping value? I am using Tomcat server v9.0.

web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">

<display-name>Spring3 MVC Application</display-name>

<servlet>
    <servlet-name>spring-web</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-web</servlet-name>
    <url-pattern></url-pattern> <!-- root request -->
    <url-pattern>/endpoint/*</url-pattern> <!-- /endpoint/${name} -->
</servlet-mapping>

spring-web-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    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-3.2.xsd
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.api.web.*" ></context:component-scan>

    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix">
            <value>/</value>
        </property>
        <property name="suffix">
            <value></value>
        </property>
    </bean>

    <mvc:resources mapping="/resources/**" location="/resources/" />

    <mvc:annotation-driven />

</beans>
Jay Sky
  • 133
  • 1
  • 1
  • 8
John Smith
  • 401
  • 6
  • 17

3 Answers3

0

There are couple of <url-pattern></url-pattern> mentioned, remove one of them and try instead of /* with / only.

This will might work.

Atul
  • 3,043
  • 27
  • 39
0

I don't understand the need for empty <url-pattern></url-pattern> pattern. But it seems that the first one is getting matched before the actual pattern can be reached. Use this setting:

<servlet-mapping>
    <servlet-name>spring-web</servlet-name>
    <url-pattern>/endpoint/*</url-pattern> <!-- /endpoint/${name} -->
    <url-pattern></url-pattern> <!-- root request -->
</servlet-mapping>
Yasin
  • 1,906
  • 1
  • 21
  • 37
  • As far as I know empty is used for handling requests on root ("/" url). Even if I swap them both or remove the empty one "endpoint/whatever" is not responding. – John Smith Dec 02 '19 at 15:46
  • So why don't you add `/` for the root? – Yasin Dec 02 '19 at 17:28
0

So the problem was that <url-pattern>/endpoint/*</url-pattern> is considered a base which should be left our from RequestMapping controller. When used with @RequestMapping(value = "/endpoint/{name}", method = RequestMethod.GET), the url where controller gets triggered is concatenated version of the two - /endpoint/endpoint/{name}. The correct RequestMapping should therefore be @RequestMapping(value = "/{name}", method = RequestMethod.GET).

John Smith
  • 401
  • 6
  • 17