Java SpringBoot始终返回404状态

ogq8wdun  于 2022-11-27  发布在  Java
关注(0)|答案(2)|浏览(137)

我是完全新的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>
xmq68pz9

xmq68pz91#

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

jexiocij

jexiocij2#

我想你得把它画出来

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

相关问题