Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring DispatcherServlet: No mapping found for HTTP request

I have an issue implementing a very simple page using spring mvc 3.2.4.RELEASE.

My controller looks like this:

@Transactional
@Controller
public class MembersDetailsController {
    @Autowired
    private MemberService memberService;

    @RequestMapping(value = "/member/{name}", method = RequestMethod.GET)
    public String displayMember(@PathVariable String name) {
        System.out.println(name);
        return "member";
    }

    @RequestMapping(value = "/member", method = RequestMethod.GET)
    public String displayMember() {
        System.out.println("Empty");
        return "member";
    }
}

When I call

http://127.0.0.1:8080/member

the respective method is being executed as desired. However, wenn I call

http://127.0.0.1:8080/member/test

or

http://127.0.0.1:8080/member/test/

I get a 404 with the log-output:

WARN  org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/member/test] in DispatcherServlet with name 'mvc-dispatcher'

Whats really weired is the a previous log says:

INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/member/{name}],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String 
INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/member],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String

Which means that the mapping should be correct as far as I understand this.

This is my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <display-name>Mitgliederdatenbank</display-name>

    <!--Configuration-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-security.xml, /WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        <init-param>
            <param-name>sessionFactoryBeanName</param-name>
            <param-value>hibernateSessionFactory</param-value>
        </init-param>
    </filter>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <!--Spring Security Filter-->
    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!--Context Loader-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- Servlets -->
    <servlet>
        <servlet-name>springGwtRemoteServiceServlet</servlet-name>
        <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
    </servlet>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <!-- Mapping -->
    <servlet-mapping>
        <servlet-name>springGwtRemoteServiceServlet</servlet-name>
        <url-pattern>/ui/springGwtServices/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/welcome</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/logout</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/loginfailed</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/member/*</url-pattern>
    </servlet-mapping>

    <!-- Default page to serve -->
  <welcome-file-list>
    <welcome-file>/login</welcome-file>
  </welcome-file-list>

</web-app>

Could anybody please give me a hint what went wrong here?

like image 717
Randy Avatar asked Sep 30 '13 16:09

Randy


People also ask

How do I resolve no mapping?

Make sure you create your package and classes in src/main/java NOT in src/main/resources. If maven doesn't create the src/main/java folder, just create the folder and move the package folders/classes there. Doing this resolved the no mapping error for me. Hope this information will be useful to someone.

How does Spring MVC handle 404 error?

The 404 error code is configured properly, but it will caused the “. htm” extension handling conflict between the “servlet container” and Spring's “DispatcherServlet“. To solve it, try change the 404. htm to other file extension, for example 404.

How can an HTTP 404 status code be returned from a Spring MVC controller?

Spring MVC is a traditional application built using the Front Controller Pattern. DispatcherServlet, which acts as the front controller, is responsible for routing and request processing. As with any web application or website, Spring MVC returns the HTTP 404 response code when the requested resource can't be found.

What is initializing Spring DispatcherServlet DispatcherServlet?

Updated on 17 October, 2021 in Spring MVC. Like other servlets, in order for the DispatcherServlet to receive and process requests, we need to configure the web server container to initialize and map the URL to it.


1 Answers

I believe that the problem is the same one as described here URL Mapping issue - Spring web MVC .

Unless you use alwaysUseFullPath spring mvc will match * part to the mapping you specified (e.g. /member/member/test ). See docs for alwaysUseFullPath here (section 17.4) http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html .

However, unfortunately, this property is not exposed through xml configuration element (if you're using xml configuration) so if you'd like your mappings to work the way you discribed in your question you'll need to configure it as described here: http://blog.sarathonline.com/2013/07/enable-alwaysusefullpath-with.html

like image 119
Krešimir Nesek Avatar answered Sep 28 '22 20:09

Krešimir Nesek