控制器无法正常工作,在白色标签错误中显示错误

我正在观看有关如何在春季创建REST控制器的视频教程。 但是,当我创建视频课程中所示的TopicController时,出现白色标签错误页面错误。我无法解决。

这是我的TopicController

package spring.boot.topic;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TopicController {

    @Autowired
    private TopicService topicService;

    @RequestMapping("/topic")
    public List<Topic> getAllTopics() {
        return topicService.getAllTopics();
    }
}

这是我的TopicService

package spring.boot.topic;

import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Service;

@Service
public class TopicService {

    private List<Topic> topics=Arrays.asList(
            new Topic("spring", "Spring Framework", "Spring Framework Description"),
            new Topic("Java", "Java Core", "Java Core Description")
            );

    public List<Topic> getAllTopics(){
        return topics;
    }
    }

这是我的主题课

package spring.boot.topic;

public class Topic {

    private String id;
    private String name;
    private String description;

    public Topic() {

    }

    public Topic(String id, String name, String description) {
        super();
        this.id = id;
        this.name = name;
        this.description = description;
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }

}

pom.xml文件

<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>
  <groupId>com.demo</groupId>
  <artifactId>spring-boot-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
</parent>
  <dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>  
</project>

在控制台中输出

:: Spring Boot ::        (v2.2.2.RELEASE)

2020-05-09 16:09:24.519  INFO 8256 --- [           main] spring.boot.api.CourseApiApp             : Starting CourseApiApp on DESKTOP-JBQF1EV with PID 8256 (C:\Users\spars\eclipse-workspace\spring-boot-api\target\classes started by spars in C:\Users\spars\eclipse-workspace\spring-boot-api)
2020-05-09 16:09:24.528  INFO 8256 --- [           main] spring.boot.api.CourseApiApp             : No active profile set, falling back to default profiles: default
2020-05-09 16:09:28.549  INFO 8256 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-05-09 16:09:28.579  INFO 8256 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-05-09 16:09:28.579  INFO 8256 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-05-09 16:09:28.761  INFO 8256 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-05-09 16:09:28.761  INFO 8256 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3908 ms
2020-05-09 16:09:29.039  INFO 8256 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-05-09 16:09:29.620  INFO 8256 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-05-09 16:09:29.623  INFO 8256 --- [           main] spring.boot.api.CourseApiApp             : Started CourseApiApp in 6.686 seconds (JVM running for 7.891)
2020-05-09 16:09:54.896  INFO 8256 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-05-09 16:09:54.897  INFO 8256 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-05-09 16:09:54.922  INFO 8256 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 24 ms

当我在Eclipse中运行程序并导航到浏览器中的localhost:8080 / topic时,出现错误

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

Sat May 09 16:45:04 IST 2020
There was an unexpected error (type=Not Found, status=404).
No message available
评论
詹驰婷
詹驰婷

Since you are using a Rest service, you can not expect to receive html file as the response. Change @RestController to @Controller and return a view model object.

点赞
评论
Zz兮兮
Zz兮兮

Your code is working fine. Try to rebuild the project by command mvn clean install first and then rerun the app.

点赞
评论