运行 localhost:8080 时出现错误“Whitelabel Error Page”

Gol*_*olu 0 java spring intellij-idea maven spring-boot

我开始学习 Spring Framework 并在运行 localhost:8080 时出现这种错误

Whitelabel 错误页面 此应用程序没有 /error 的显式映射,因此您将其视为后备。

控制器.java

package com.springFramework.helloGame.controller;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

    @GetMapping("/sum")
    public long displaySum(){
        return 100;
    }

}
Run Code Online (Sandbox Code Playgroud)

HelloGameApplication.java

package com.springFramework.helloGame;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
public class HelloGameApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context =
                SpringApplication.run(HelloGameApplication.class, args);
    }

}
Run Code Online (Sandbox Code Playgroud)

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.springFramework</groupId>
    <artifactId>helloGame</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>helloGame</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </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-web</artifactId>
        </dependency>

    </dependencies>

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

</project>
Run Code Online (Sandbox Code Playgroud)

项目结构

我的项目结构看起来不错,但不知道为什么出现错误!请帮我!

小智 6

Whitelabel 页面是一种 spring 错误处理方法,它尝试提示您确实有 /error 的映射。现在谈到这个问题,您需要在控制器中的 get 映射上方编写 @RequestMapping("/") ,以便 spring 知道基本 URI。它是浏览器的 spring 应用程序的入口点。

现在您已经编写了 /sum get URI 并且在浏览器中,您正在尝试 http://localhost:8080 因此,要使上述 Uri 工作,您需要提供 @RequestMapping("/")

或者您可以将 http://localhost:8080/sum 与当前代码一起使用。