java - SpringBoot内嵌tomcat依赖

标签 java spring-mvc tomcat

我使用 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/

相关文章:

java - 在 servlet (Java/Tomcat/Linux) 中使用我自己的类

java - Eclipse 找不到 Spring 配置文件

java - Tomcat 运行但在尝试访问我的 html 时给出 404,但在安装后显示 tomcat 的默认页面

java - Tomcat 身份验证和 Spring Security

java - 将bean注入(inject)到spring java配置类中

java - 运行 java servlet 时出现 Classnotfoundexception

java - 如何在Java中将txt文件转换为数组,以便我可以单独处理每个数组

java - Eclipse VisualSVN 错误 svn E175002 : java. lang.NumberFormatException

java - 递归地在空格中添加星号

java - Spring Security AOP错误