Spring MVC 白色标签错误页面[Sping Boot 3][@RequestMapping] [已关闭]

huwehgph  于 2023-03-08  发布在  Spring
关注(0)|答案(1)|浏览(154)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
6天前关闭。
Improve this question
我正在尝试从ContentCollectionRepository获取记录类存储信息,并将其显示在Web浏览器中。我正在使用@RequestMapping定义查找此资源所遵循的特定路径,但我不断收到“白标签错误页,此应用程序没有/error的显式Map,因此您将其视为一个回退。
2023年2月28日星期二20:37:28 EST出现意外错误(类型=未找到,状态=404)。”
我什么都试过了,好像都不管用,有什么主意吗?
内容集合存储库**

package com.cristianabv1993.contentcalendar.repository;

import com.cristianabv1993.contentcalendar.model.Content;
import com.cristianabv1993.contentcalendar.model.Status;
import com.cristianabv1993.contentcalendar.model.Types;
import jakarta.annotation.PostConstruct;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Repository
public class ContentCollectionRepository {

    private final List<Content> content = new ArrayList<>();

    public ContentCollectionRepository(){

    }

    public List<Content> findAll(){
        return content;
    }

    public Optional<Content> findById(Integer id){
        return content.stream().filter(c -> c.id().equals(id)).findFirst();
    }

    @PostConstruct
    private void init(){
        Content c = new Content(
                1,"My first blog post","My first blog post", Status.IDEA, Types.ARTICLE,
                LocalDateTime.now(),
                null,
                ""
        );

        content.add(c);
    }
}

内容控制器**

import com.cristianabv1993.contentcalendar.model.Content;
import com.cristianabv1993.contentcalendar.repository.ContentCollectionRepository;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/content")
public class ContentController {

    private final ContentCollectionRepository repository;

    public ContentController(ContentCollectionRepository repository) {
        this.repository = repository;
    }

    //make a request and find all the pieces of content in the system
    @GetMapping("/")
    public List<Content> findAll(){
        return repository.findAll();
    }
}

我的记录类与信息,我试图显示。

import java.time.LocalDateTime;

public record Content(
        Integer id,
        String title,
        String desc,
        Status status,
        Types contentType,
        LocalDateTime dateCreated,
        LocalDateTime dateUpdated,
        String url
) {
}

localhost:8080/API/content之后的堆栈跟踪无法显示信息

/home/cristianb/.jdks/openjdk-19.0.2/bin/java -javaagent:/app/idea-IC/lib/idea_rt.jar=33845:/app/idea-IC/bin -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath /home/cristianb/spring boot/CalendarSpringBoot3/content-calendar/target/classes:/home/cristianb/.m2/repository/org/springframework/boot/spring-boot-starter-web/3.0.3/spring-boot-starter-web-3.0.3.jar:/home/cristianb/.m2/repository/org/springframework/boot/spring-boot-starter/3.0.3/spring-boot-starter-3.0.3.jar:/home/cristianb/.m2/repository/org/springframework/boot/spring-boot/3.0.3/spring-boot-3.0.3.jar:/home/cristianb/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/3.0.3/spring-boot-autoconfigure-3.0.3.jar:/home/cristianb/.m2/repository/org/springframework/boot/spring-boot-starter-logging/3.0.3/spring-boot-starter-logging-3.0.3.jar:/home/cristianb/.m2/repository/ch/qos/logback/logback-classic/1.4.5/logback-classic-1.4.5.jar:/home/cristianb/.m2/repository/ch/qos/logback/logback-core/1.4.5/logback-core-1.4.5.jar:/home/cristianb/.m2/repository/org/apache/logging/log4j/log4j-to-slf4j/2.19.0/log4j-to-slf4j-2.19.0.jar:/home/cristianb/.m2/repository/org/apache/logging/log4j/log4j-api/2.19.0/log4j-api-2.19.0.jar:/home/cristianb/.m2/repository/org/slf4j/jul-to-slf4j/2.0.6/jul-to-slf4j-2.0.6.jar:/home/cristianb/.m2/repository/jakarta/annotation/jakarta.annotation-api/2.1.1/jakarta.annotation-api-2.1.1.jar:/home/cristianb/.m2/repository/org/yaml/snakeyaml/1.33/snakeyaml-1.33.jar:/home/cristianb/.m2/repository/org/springframework/boot/spring-boot-starter-json/3.0.3/spring-boot-starter-json-3.0.3.jar:/home/cristianb/.m2/repository/com/fasterxml/jackson/core/jackson-databind/2.14.2/jackson-databind-2.14.2.jar:/home/cristianb/.m2/repository/com/fasterxml/jackson/core/jackson-annotations/2.14.2/jackson-annotations-2.14.2.jar:/home/cristianb/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.14.2/jackson-core-2.14.2.jar:/home/cristianb/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jdk8/2.14.2/jackson-datatype-jdk8-2.14.2.jar:/home/cristianb/.m2/repository/com/fasterxml/jackson/datatype/jackson-datatype-jsr310/2.14.2/jackson-datatype-jsr310-2.14.2.jar:/home/cristianb/.m2/repository/com/fasterxml/jackson/module/jackson-module-parameter-names/2.14.2/jackson-module-parameter-names-2.14.2.jar:/home/cristianb/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/3.0.3/spring-boot-starter-tomcat-3.0.3.jar:/home/cristianb/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/10.1.5/tomcat-embed-core-10.1.5.jar:/home/cristianb/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/10.1.5/tomcat-embed-el-10.1.5.jar:/home/cristianb/.m2/repository/org/apache/tomcat/embed/tomcat-embed-websocket/10.1.5/tomcat-embed-websocket-10.1.5.jar:/home/cristianb/.m2/repository/org/springframework/spring-web/6.0.5/spring-web-6.0.5.jar:/home/cristianb/.m2/repository/org/springframework/spring-beans/6.0.5/spring-beans-6.0.5.jar:/home/cristianb/.m2/repository/io/micrometer/micrometer-observation/1.10.4/micrometer-observation-1.10.4.jar:/home/cristianb/.m2/repository/io/micrometer/micrometer-commons/1.10.4/micrometer-commons-1.10.4.jar:/home/cristianb/.m2/repository/org/springframework/spring-webmvc/6.0.5/spring-webmvc-6.0.5.jar:/home/cristianb/.m2/repository/org/springframework/spring-aop/6.0.5/spring-aop-6.0.5.jar:/home/cristianb/.m2/repository/org/springframework/spring-context/6.0.5/spring-context-6.0.5.jar:/home/cristianb/.m2/repository/org/springframework/spring-expression/6.0.5/spring-expression-6.0.5.jar:/home/cristianb/.m2/repository/org/slf4j/slf4j-api/2.0.6/slf4j-api-2.0.6.jar:/home/cristianb/.m2/repository/org/springframework/spring-core/6.0.5/spring-core-6.0.5.jar:/home/cristianb/.m2/repository/org/springframework/spring-jcl/6.0.5/spring-jcl-6.0.5.jar com.cristianabv1993.contentcalendar.ContentCalendarApplication

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v3.0.3)

2023-03-01T20:06:56.864-05:00  INFO 387 --- [           main] c.c.c.ContentCalendarApplication         : Starting ContentCalendarApplication using Java 19.0.2 with PID 387 (/home/cristianb/spring boot/CalendarSpringBoot3/content-calendar/target/classes started by cristianb in /home/cristianb/spring boot/CalendarSpringBoot3/content-calendar)
2023-03-01T20:06:56.868-05:00  INFO 387 --- [           main] c.c.c.ContentCalendarApplication         : No active profile set, falling back to 1 default profile: "default"
2023-03-01T20:06:57.480-05:00  INFO 387 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-03-01T20:06:57.487-05:00  INFO 387 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-03-01T20:06:57.487-05:00  INFO 387 --- [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.5]
2023-03-01T20:06:57.553-05:00  INFO 387 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-03-01T20:06:57.554-05:00  INFO 387 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 642 ms
2023-03-01T20:06:57.778-05:00  INFO 387 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-03-01T20:06:57.787-05:00  INFO 387 --- [           main] c.c.c.ContentCalendarApplication         : Started ContentCalendarApplication in 1.233 seconds (process running for 1.504)
2023-03-01T20:07:02.889-05:00  INFO 387 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-03-01T20:07:02.889-05:00  INFO 387 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-03-01T20:07:02.890-05:00  INFO 387 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
kd3sttzy

kd3sttzy1#

由于我复制了您的问题,并且在启动应用程序时没有看到任何问题,因此我认为该问题发生在您尝试调用API时。
让我们来看看GET方法:

@GetMapping("/")
public List<Content> findAll(){
  return repository.findAll();
}

请检查您是否调用了http://localhost:8080/api/content而不是http://localhost:8080/api/content/
在这种情况下,/决定了成功调用还是404 not found

相关问题