Java 控制器不工作,在白标签错误中显示错误
我正在观看如何在春季创建REST控制器的视频教程。 但是,当我创建一个TopicController,如视频课程中所示时,我得到了一个Whitelabel错误页面的错误。我无法解决它 这是我的主题控制器Java 控制器不工作,在白标签错误中显示错误,java,spring,spring-boot,maven,tomcat,Java,Spring,Spring Boot,Maven,Tomcat,我正在观看如何在春季创建REST控制器的视频教程。 但是,当我创建一个TopicController,如视频课程中所示时,我得到了一个Whitelabel错误页面的错误。我无法解决它 这是我的主题控制器 package spring.boot.topic; import java.util.Arrays; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import o
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();
}
}
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>
当我在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
你的代码运行良好。请先尝试通过命令mvn clean install重新生成项目,然后重新运行应用程序。由于您使用的是Rest服务,因此无法期望收到html文件作为响应。将
@RestController
更改为@Controller
并返回视图模型对象。请发布出现的错误
<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
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