我使用 spring initializer 创建了一个简单的 spring boot web 项目。我有一个简单的项目设置,其中包括以下依赖项:
compile('org.springframework.boot:spring-boot-starter-jersey')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
我使用 JSP 作为我的 View ,所以我在我的配置中添加了以下 bean:
@Bean
public ViewResolver getViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
return resolver;
}
当我启动应用程序并转到主页时,出现 404 错误,但是,当我将以下 JAR 添加到我的类路径时
compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '7.0.77'
它有效。我的印象是spring boot starter web依赖包含了嵌入式tomcat,我不需要单独添加它?
这是启动它的主要应用程序类:
@SpringBootApplication(scanBasePackages={"com.wsapp"})
@EnableWebMvc
@EnableAutoConfiguration
public class SampleApplication {
public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}
}
最佳答案
如果您希望使用 tomcat,为什么要在依赖项中使用 jersey?
compile('org.springframework.boot:spring-boot-starter-jersey')
我认为 spring 自动配置只是加载 Jersey 配置。 Tomcat 是默认的,但前提是您没有在依赖项中明确添加另一个模块。如果您想使用 tomcat,请尝试删除此依赖项。
关于java - SpringBoot内嵌tomcat依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43720641/