简体   繁体   中英

Facing o.s.web.servlet.PageNotFound : No mapping for GET / problem in my spring boot project

I am trying to implement REST microservices using Spring Boot in my project. I have created a controller for login page and also created repository. But while hitting URL I am facing this issue o.s.web.servlet.PageNotFound: No mapping for GET /

Main Class

@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);
    }

}

Controller

@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";
    }
}

Repository

@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>

Please help me to find solution. Thank You

Your controller is probably not being picked up by the ComponentScan because you have limited to the package for the scan.

Try this:

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

And This:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM