【问题标题】:Java SpringBoot Always Returning 404 StatusJava SpringBoot 总是返回 404 状态
【发布时间】:2022-11-26 13:39:42
【问题描述】:

我是 SpringBoot 的新手,我在 YT 上学习了一门课程。

要求:

演示应用程序.java

package com.sbt.demo;

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

@SpringBootApplication
@RestController
public class DemoApplication {

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

  @GetMapping
  public String hello() {
    return "Hello World";
  }
}

在课程中,他能够得到“Hello World”的回应,但我不断得到

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Nov 25 16:24:31 CST 2022
There was an unexpected error (type=Not Found, status=404).

我已经像其他类似问题一样添加了 @RestController 注释,并提供了建议的答案,但我一直收到错误消息。

我还重新启动了服务器并重试并不断收到错误消息。

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>3.0.0</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.sbt</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>17</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>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

【问题讨论】:

  • 你发送了什么请求?

标签: java spring-boot


【解决方案1】:

请也分享您的 pom.xml 和 application.properties。因为这段代码看起来正确。也许这些文件中可能存在问题。

【讨论】:

  • 添加了 pom 文件。
猜你喜欢
  • 2020-02-09
  • 2017-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 2015-07-31
相关资源
最近更新 更多