【问题标题】:Facing o.s.web.servlet.PageNotFound : No mapping for GET / problem in my spring boot project面对 os.web.servlet.PageNotFound:我的 spring boot 项目中没有 GET / 问题的映射
【发布时间】:2021-03-29 15:32:17
【问题描述】:

我正在尝试在我的项目中使用 Spring Boot 实现 REST 微服务。我为登录页面创建了一个控制器,还创建了存储库。 但是在点击 URL 时,我遇到了这个问题 o.s.web.servlet.PageNotFound: No mapping for GET /

主类

@ComponentScan(basePackages = {"com.springboot.controller.repository"})
@EntityScan(basePackages="com.springboot.controller.model")
@EnableJpaRepositories(basePackages="com.springboot.controller.repository")

@SpringBootApplication

public class CunsultustodayWebServicesApplication {

    public static void main(String[] args) {
        SpringApplication.run(CunsultustodayWebServicesApplication.class, args);
    }

}

控制器

@RestController

@ComponentScan

@Controller

public class LoginController {

    @Autowired(required=true)
    private UserRepo userrepo;
    
    @RequestMapping("/")
    public String checkMVC()
    {
        return "Login";
    }
    
    @RequestMapping("/login")
    public String loginHome(@RequestParam("email") String email, @RequestParam("password") String password, Model model)
    {
        User u = null;
        try {
            u= userrepo.findByEmail(email);
        }
        catch(Exception ex) {
            System.out.println("User Not Found!!!");
        }
        if(u!=null) {
          model.addAttribute("email", email);
          return "HomePage";
        }
        return "Login";
    }
}

存储库

@Service("UserRepo")

public interface UserRepo extends JpaRepository<User,Integer> {

    User findByEmail(String email);

}

pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springboot.controller</groupId>
    <artifactId>cunsultustoday-web-services</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cunsultustoday-web-services</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
           <groupId>org.apache.tomcat.embed</groupId>
           <artifactId>tomcat-embed-jasper</artifactId>
           <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.9</version>
        </dependency>
        <dependency>
           <groupId>javax.servlet</groupId>
           <artifactId>jstl</artifactId>
           <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-redis</artifactId>
                <exclusions>
                    <exclusion>
                        <groupId>io.lettuce</groupId>
                        <artifactId>lettuce-core</artifactId>
                    </exclusion>
                </exclusions>    
            </dependency>
            <dependency>
                <groupId>redis.clients</groupId>
                <artifactId>jedis</artifactId>
            </dependency>
            <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-webmvc</artifactId>
              </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

请帮助我找到解决方案。谢谢你

【问题讨论】:

  • 你有一些包裹吗?
  • 您已将组件扫描放在仅包含存储库包的 Spring Boot 应用注释类上,如果您有其他带有控制器或其他包的包,它们将不会被扫描,您可能会因此而面临问题。
  • 感谢您的建议。

标签: java spring spring-boot


【解决方案1】:

您的控制器可能没有被 ComponentScan 拾取,因为您仅限于扫描包。

试试这个:

// note @ComponentScan is removed as it is included already in @SpringBootApplication
@EnableJpaRepositories
@SpringBootApplication
public class CunsultustodayWebServicesApplication {
...

还有这个:

// note @Controller and @CompnentScan are removed
@RestController
public class LoginController {

【讨论】:

  • 好的,我会做这个改变。感谢您的建议。
  • 它对我有用。
猜你喜欢
  • 2021-04-29
  • 2021-05-06
  • 2019-09-26
  • 2018-06-01
  • 2016-02-17
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 2020-06-26
相关资源
最近更新 更多