【问题标题】:Getting error "Whitelabel Error Page" while running localhost:8080运行 localhost:8080 时出现错误“Whitelabel Error Page”
【发布时间】:2021-11-12 08:09:03
【问题描述】:

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

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

Controller.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;
    }

}

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

}

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>

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

【问题讨论】:

  • while running localhost:8080 是什么意思(您是否尝试联系/sum)?请同时提供所有日志打印件

标签: java spring spring-boot maven intellij-idea


【解决方案1】:

您的控制器似乎只有“/sum”路径的映射。尝试查询 localhost:8080/sum。

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-21
      • 2020-01-26
      • 2021-03-01
      • 2019-09-10
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 2019-05-03
      相关资源
      最近更新 更多