【发布时间】:2015-02-03 15:07:44
【问题描述】:
如果我调用我的 tomcat 服务器的默认 url,我会看到错误 404。程序运行没有任何问题,但可能是 restcontroller 被禁用(我不知道,通常不会)。日志中没有错误。起始页是 html 格式。我设置了默认路径(见下文)。
我正在使用 Spring Boot 和 tomcat7。
请求的资源 (/dashboard/) 不可用。
Spring Boot 路径 src\main\resources\templates\index.html
战争中的Index.html路径: ..\tomcat7\webapps\dashboard\WEB-INF\classes\templates\index.html
主要
@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.###.dashboard")
@EnableJpaRepositories
@EntityScan(basePackages = "com.###.dashboard.domain")
public class Dashboard extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(new Object[] {Dashboard.class, DashboardController.class}, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Dashboard.class);
}
}
RestController
@RestController
public class DashboardController {
/**
*...
*/
@RequestMapping("/")
public String setPage(Model model) {
// ...
return "index";
}
}
pom
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.###.dashboard</groupId>
<artifactId>Dashboard</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>Dashboard</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mobile</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<properties>
<start-class>com.###.dashboard.main.Dashboard</start-class>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
tomcat7 服务器
2014-12-05 13:31:46.836 INFO 28052 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/],methods=[],param
s=[],headers=[],consumes=[],produces=[],custom=[]}" onto public java.lang.String com.###.dashboard.domain.DashboardController.setPage(org.springframework.ui.Model)
谢谢
【问题讨论】:
-
向“/”发出请求时会发生什么?
-
/ = 白页,/dashboard/ = 请求的资源 (/dashboard/) 不可用。
-
也许不是问题,但听起来你只想要
@Controller而不是@RestController。 RestController 用于直接处理序列化数据,而不是通过 View 解析器 -
我昨天试过@Controller,同样的问题
-
我遇到了同样的问题。结果我忘了在 pom.xml 文件中添加
spring-boot-starter-thymeleaf依赖项,但我可以看到你这样做了......你是否尝试过进入 pom.xml 文件并运行mvn clean install package?下载的依赖可能与 pom 文件不同步。
标签: java spring maven spring-mvc tomcat