Getting “WARN org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI …” when trying to setup spring servlet

后端 未结 3 472
萌比男神i
萌比男神i 2021-01-25 07:56

I am trying to setup a Spring MVC project. I have added a dispatcher servlet, a jsp and setup the web.xml file. But I keep getting

WARN org.springframew

相关标签:
3条回答
  • 2021-01-25 08:30

    I got that error when i migrated/updated from Spring framework 3.X to 4.X and using json (through Jackson2) as returned object and got the error above.

    To solve that you should add

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                    <property name="supportedMediaTypes" value = "text/plain;charset=UTF-8"/>
                </bean>
            </list>
        </property>
    </bean>
    
    <bean  class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
        <property name="mediaTypes">
            <map>
               <entry key="json" value="application/json"/>
           </map>
        </property>
    </bean>
    
    <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView">
        <property name="contentType" value="application/json"/>
    </bean>
    

    To XXX-servlet.xml file

    And on the code you should use MappingJackson2JsonView as return value for example :

    @RequestMapping("/user")
    MappingJackson2JsonView user() {
    MappingJackson2JsonView view = new MappingJackson2JsonView();
    view.setExtractValueFromSingleKeyModel(true);
    view.addStaticAttribute("user", new User("Sébastien", "Deleuze"));
    view.setPrefixJson(true);
    return view;
    }
    

    for more examples look here: http://www.programcreek.com/java-api-examples/index.php?api=org.springframework.web.servlet.view.json.MappingJackson2JsonView

    0 讨论(0)
  • 2021-01-25 08:34

    I had a similar issue before which was very confusing, after a series of tests, I found it's the url-pattern for the DispatcherServlet. Be cautious when you use the asterisk for wildcard match which might lead to unexpected behavior, and it's just safe to start from root "/" or your custom servlet context path "/foo/"

    Try the following.

    <servlet-mapping>
      <servlet-name>HelloWeb</servlet-name>
      <url-pattern>/</url-pattern>
    </servlet-mapping>

    0 讨论(0)
  • 2021-01-25 08:39

    You seem to be missing the <mvc:annotation-driven />

    <?xml version="1.0" encoding="UTF-8"?>
    <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.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <!-- Enables the Spring MVC @Controller programming model -->
        <mvc:annotation-driven />
    
        <context:component-scan base-package="org.activiti.explorer.controller" />
    
        <bean id="viewResolver"
              class="org.springframework.web.servlet.view.UrlBasedViewResolver">
            <property name="viewClass"
                      value="org.springframework.web.servlet.view.JstlView" />
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    </beans>
    

    note that I've removed the version from the xsd files, this means that it will use the schema from your jar files (and there will be a validation error in case of incompatibilty)

    after @Nikolay's comment, I've also noticed an error in your mapping (note that you still need the annotation-driven element), you should either change the mapping in your controller to

    @RequestMapping("/hello.jsp")
    

    and access it via

    /safesite/hello.jsp
    

    OR, more common, change the servlet mapping to

    <servlet-mapping>
        <servlet-name>HelloWeb</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    and access as Nikolay said so /safesite/hello

    0 讨论(0)
提交回复
热议问题