我看到白色标签错误页面,状态为404,而不是字符串"所有电影!" 我在http://localhost:8080/api/v1/movies上发出GET请求

我的控制器

package pl.krywion.movies.app;

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

@RestController
@RequestMapping("/api/v1/movies")
public class MovieController {
    @GetMapping
    public String allMovies() {
        return "All Movies!";
    }
}

我的主要班级

package pl.krywion.movies;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MoviesApplication {

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

}
my pom.xml file

<?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.3</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>pl.krywion</groupId>
    <artifactId>movies</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>movies</name>
    <description>movies</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>me.paulschwarz</groupId>
            <artifactId>spring-dotenv</artifactId>
            <version>2.5.4</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

我的应用程序.属性

spring.data.mongodb.database=${env.MONGO_DATABSE}
spring.data.mongodb.uri=mongodb+srv://${env.MONGO_USER}:${env.MONGO_PASSWORD}@${env.MONGO_CLUSTER}

我的日志(log)在调试后运行并发出请求

2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected StandardServletMultipartResolver
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected AcceptHeaderLocaleResolver
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected FixedThemeResolver
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator@26512192
2023-02-26T19:45:33.142+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Detected org.springframework.web.servlet.support.SessionFlashMapManager@1c16475
2023-02-26T19:45:33.143+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : enableLoggingRequestDetails='false': request parameters and headers will be masked to prevent unsafe logging of potentially sensitive data
2023-02-26T19:45:33.143+01:00  INFO 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 2 ms
2023-02-26T19:45:33.147+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : GET "/api/v1/movies", parameters={}
2023-02-26T19:45:33.152+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-02-26T19:45:33.157+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.w.s.r.ResourceHttpRequestHandler     : Resource not found
2023-02-26T19:45:33.157+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Completed 404 NOT_FOUND
2023-02-26T19:45:33.161+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2023-02-26T19:45:33.162+01:00 DEBUG 8118 --- [nio-8080-exec-2] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2023-02-26T19:45:33.179+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Using 'application/json', given [*/*] and supported [application/json, application/*+json]
2023-02-26T19:45:33.179+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : Writing [{timestamp=Sun Feb 26 19:45:33 CET 2023, status=404, error=Not Found, message=No message available,  (truncated)...]
2023-02-26T19:45:33.197+01:00 DEBUG 8118 --- [nio-8080-exec-2] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 404

我不知道怎么修,我是春天的初学者

我try 使用@ComponentScan注释,并将MovieApplication Java类移动到上面的包中,或者将其移动到与控制器相同的包中

推荐答案

Spring 3.0.3:https://github.com/spring-projects/spring-boot/issues/34379中的组件扫描出现问题

所以,我建议你的应用程序的路径有一些空间.

try 将你的应用程序移动到路径中没有空格的另一个文件夹,然后重新运行它.

希望它能帮助您:)

Java相关问答推荐

无法找到符号错误—Java—封装

SQlite for Android无法使用json_group_array/json_object

有没有一种方法使保持活动设置专用于java.net.http.HttpClient的一个实例

流迭代列表<;对象>;上的NoSuchElementException

Hibernate 6支持Joda DateTime吗?

查找剩余的枚举

try 判断可选参数是否为空时出现空类型安全警告

把一条整型短裤和两条短裤装成一条长的

Java泛型类方法的静态返回类型是否被类型擦除?

如何创建模块信息类文件并将其添加到JAR中?

为什么StandardOpenOption.CREATE不能通过Ubuntu在中小企业上运行?

如何以编程方式保存workBench.xmi?

JavaFX:无论何时显示应用程序,如何更改组件/ node 位置?

无法使用Open WebStart Java 8运行jnlp

Java 17与Java 8双重表示法

Intellij 2023 IDE:始终在顶部显示菜单栏

按长度排序字符串数组

Java编译器是否进行了持续的折叠优化,以及如何进行判断?

@此处不能应用可为null的批注

如何调查进程列表中不可见的活跃 MySQL 事务?