简体   繁体   English

为什么我在运行 Spring 启动项目时总是得到状态为“404”的 Whitelabel 错误页面

[英]Why do I always get Whitelabel Error Page with status “404” while running Spring Boot Project

My pom.xml with all my needed dependencies like spring boot and its web dependencies我的 pom.xml 以及我需要的所有依赖项,例如 spring 启动及其 web 依赖项

<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>a-LABO</groupId>
  <artifactId>a-LABO</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.7.0</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.2.RELEASE</version>
    </parent>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

</project>

My Controller class:我的 Controller class:

package main.java; package main.java;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

    @RestController
    public class UserController {

        @RequestMapping("/hello")
        public String SayHi() {
            return "Hi!";
        }

    }

And here's my Main (Entry point) class where Tomcat gets started这是我的主要(入口点)class Tomcat 开始的地方

@SpringBootApplication
public class Application {

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

    @Bean
    public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
        return args -> {

            System.out.println("Let's inspect the beans provided by Spring Boot:");

            String[] beanNames = ctx.getBeanDefinitionNames();
            Arrays.sort(beanNames);
            for (String beanName : beanNames) {
                System.out.println(beanName);
            }

        };
    }

}

My UserController is in src/controllers.我的 UserController 在 src/controllers 中。 Now the problem is I always get the spring error page when I navigate to /hello.现在的问题是当我导航到 /hello 时,我总是得到 spring 错误页面。 Why is this and how can i fix it?为什么会这样,我该如何解决?

Thank you谢谢

Make sure UserController belongs to a package which is sub package of your Application's package to ensure UserController is scanned during component scan确保 UserController 属于 package,它是应用程序 package 的子 package 以确保在组件扫描期间扫描 UserController

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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