关于spring:没有找到带有基于java配置的URI的HTTP请求的映射

No mapping found for HTTP request with URI with java based configuration

本问题已经有最佳答案,请猛点这里访问。

在学习 spring 时,我试图运行 Spring in Action 的代码示例
但它在 tomcat 日志中向我显示了以下提到的错误:

1
11Aug2016 12:15:57.561 WARNING [httpnio8080exec1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/RestTester0.0.1SNAPSHOT/] in DispatcherServlet with name ‘dispatcher’

我正在尝试仅借助基于 java 的配置来配置 spring。
下面提到了相同的代码:

TestProjectInitializer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package test.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class TestProjectInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class< ? >[] getRootConfigClasses() {
        // TODO Auto-generated method stub
        return new Class< ? >[]{RootConfig.class};
    }

    @Override
    protected Class< ? >[] getServletConfigClasses() {
        // TODO Auto-generated method stub
        return new Class< ? >[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        // TODO Auto-generated method stub
        return new String[]{"/"};
    }
}

WebConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

@Configuration
@EnableWebMvc
@ComponentScan("test.webconf")
public class WebConfig extends WebMvcConfigurerAdapter{

    @Bean
    public ViewResolver getViewResolver()
    {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".html");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer)
    {
        configurer.enable();
    }
}

RootConfig.java

1
2
3
4
5
6
7
8
9
10
11
12
13
package test.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@Configuration
@ComponentScan(basePackages={"test"}, excludeFilters={@Filter(type=FilterType.ANNOTATION,value=EnableWebMvc.class)})
public class RootConfig {

}

HomeController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package test.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
/*@RequestMapping("/")*/
public class HomeController
{
    @RequestMapping(value="/",method=RequestMethod.GET)
    public String home()
    {
        return "home";
    }

}

pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>test</groupId>
  RestTester</artifactId>
  <version>0.0.1SNAPSHOT</version>
  <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            springcontext</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            springwebmvc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            springweb</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            javax.servletapi</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>  
    </dependencies>

  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        mavencompilerplugin</artifactId>
        <version>3.3</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
      <plugin>
        mavenwarplugin</artifactId>
        <version>2.6</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8"?>
<webapp xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <displayname>test</displayname>
  <welcomefilelist>
    <welcomefile>index.html</welcomefile>
    <welcomefile>index.htm</welcomefile>
    <welcomefile>index.jsp</welcomefile>
    <welcomefile>default.html</welcomefile>
    <welcomefile>default.htm</welcomefile>
    <welcomefile>default.jsp</welcomefile>
  </welcomefilelist>

</webapp>

目录结构

请帮助我理解我做错了什么。


Webmvc 上的基础包名错误,应该是 test.controller.


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/java/267517.html

(0)
上一篇 2022年6月18日 19:39
下一篇 2022年6月18日 19:39

相关推荐

发表回复

登录后才能评论