java Spring Boot 控制器 404
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37342977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot Controller 404
提问by Gaurav Bipul
I am fairly new in Spring Boot and trying to build a simple web app. I have defined a controller class containing my mapping for url, but on browser it is giving me a white label page error(404). I am not able to understand why it is not able to map. I have tried changing my component scan base package, but it still doesn't redirect me to page "abc" or print "in controller" in the console.
我是 Spring Boot 的新手,正在尝试构建一个简单的 Web 应用程序。我已经定义了一个包含我的 url 映射的控制器类,但是在浏览器上它给了我一个白标签页面错误 (404)。我无法理解为什么它无法映射。我已经尝试更改我的组件扫描基础包,但它仍然没有将我重定向到页面“abc”或在控制台中打印“in controller”。
pom.xml
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>
Controller class
控制器类
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";
}
}
Main class
主班
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);
}
}
回答by Daniel Olszewski
The BmsApplication
class is placed under the com.wellmanage.bms
package and by default the @SpringBootApplication
annotation runs component scan using this package as the root. Since the BookController
is inside com.wellmanage.controller
, the package is omitted by default configuration.
所述BmsApplication
类被置于下com.wellmanage.bms
包和默认@SpringBootApplication
注释运行使用该包作为根组件扫描。由于BookController
是 inside com.wellmanage.controller
,默认配置中省略了包。
You have two options:
您有两个选择:
- Change default component scan settings and set which package would you like to scan:
- 更改默认组件扫描设置并设置您要扫描的包:
@ComponentScan("com.wellmanage")
@SpringBootApplication
public class BmsApplication {
public static void main(String[] args) {
SpringApplication.run(BmsApplication.class, args);
}
}
- Move the main Application class to root package of you app so that all components which you want to scan automatically are under its package, e.g. to
com.wellmanage
.
- 将主应用程序类移动到您应用程序的根包,以便您要自动扫描的所有组件都在其包下,例如到
com.wellmanage
.
From the configuration you have posted, it seems you didn't set any template engine. Returning the String "abc" from your controller is hence ignored.
从您发布的配置来看,您似乎没有设置任何模板引擎。因此,从控制器返回字符串“abc”将被忽略。