我在Spring Boot中还很陌生,正在尝试构建一个简单的Web应用程序。我已经定义了一个控制器类,其中包含我对url的映射,但是在浏览器上却给了我一个白色标签页错误(404)。我无法理解为什么无法映射。我已经尝试过更改组件扫描基本程序包,但是它仍然不能将我重定向到页面“ abc”或在控制台中打印“在控制器中”。
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>bms</groupId> <artifactId>com.wellmanage.bms</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>BMS</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.5.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <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> </project>
控制器类
package com.wellmanage.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class BookController { @RequestMapping("/") public String hello() { System.out.println("in controller"); return "abc"; } }
主班
package com.wellmanage.bms; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class BmsApplication { public static void main(String[] args) { SpringApplication.run(BmsApplication.class, args); } }
所述BmsApplication类被置于下com.wellmanage.bms包和默认@SpringBootApplication注释运行使用该包作为根组件扫描。由于BookController位于内部com.wellmanage.controller,因此默认配置中省略了该软件包。
BmsApplication
com.wellmanage.bms
@SpringBootApplication
BookController
com.wellmanage.controller
您有两种选择:
更改默认组件扫描设置并设置您要扫描的软件包:
@ComponentScan("com.wellmanage") @SpringBootApplication public class BmsApplication { public static void main(String[] args) { SpringApplication.run(BmsApplication.class, args); } }
将主Application类移动到应用程序的根包中,以便要自动扫描的所有组件都位于其包下,例如com.wellmanage。
com.wellmanage
从您发布的配置来看,您似乎没有设置任何模板引擎。因此,将忽略从控制器返回的字符串“ abc”。