spring - spring @controller vs @RestController 错误 whitelabel 错误
问题描述
我在 STS IDE 中创建了一个基本的 Spring MVC 项目。当我尝试添加控制器时,我添加注释 @Controller 并运行 spring boot 程序,我收到以下错误:-(Whitelabel Error Page This application has no explicit mapping for /error,因此您将此视为后备。 ),但是当我添加 @RestController 时,该方法返回“hello”字符串。请帮助我解决如何使用 @Controller 修复它,并请让我知道为什么会发生这种情况。
示例代码:
package com.adithya.spring.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Controller
public class ApplicationController {
@RequestMapping("/")
public String Hello() {
return "Hello";
}
}
另外,我的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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.adithya</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>MyApplication</name>
<description>Login & Reg</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
解决方案
推荐阅读
- python - 比较两个数据框列并打印 df1 中的不常见值
- css - 我需要为表单字段定制 CSS
- c - 如何解释以下代码的输出?(与 C 中的类型转换有关)
- java - 如何让 Log4j2 RollingFileAppender 创建新的日志文件并滚动现有文件
- python - 在python中使用类列表绘制图形
- html - 如何使用 CSS 属性位置添加我的图标:绝对,以便它始终保留在我添加它的密码字段中
- sql - 如何在 Oracle 插入命令中使用双表中的子查询?
- wordpress - 如何在 wordpress 的新小部件中显示简码?
- python - 如何修改熊猫数据框的日期时间对象列中的分钟数?
- python - python pandas查询列表中的值