spring - 如何在tomcat 8下运行Spring RESTful项目?

标签 spring intellij-idea gradle spring-boot tomcat8

我正在关注下一个教程:https://spring.io/guides/gs/rest-service/

我注意到,thay不要使用tomcat来运行他们的项目。他们使用“gradle run”命令来运行它。
是否可以在tomcat下运行该Spring项目?

另外,我想知道它是否合理。我们是否需要tomcat来运行spring RESTful项目(也许最好使用“gradle run”命令来运行)?

我正在使用Intellij IDEA和gradle 3.0

我有下一个build.gradle内容:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'

jar {
    baseName = 'gs-rest-service'
    version =  '0.1.0'
}

repositories {
    jcenter()
    maven { url "http://repo.spring.io/libs-snapshot" }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}

所有类(class)都类似于本教程

最佳答案

如果要使用Spring Boot提供的嵌入式Tomcat,则可以只使用spring-boot-starter-web入门程序,并使用spring-boot:run运行项目。

但是,当您按提供的方式设置spring-boot-starter-tomcat时,我认为您想使用外部Tomcat。

在这里您可以找到用于创建WAR的documentation。您需要像以前一样使用WAR gradle插件,配置Tomcat实例并使用spring-boot:run运行项目。

If you’re using Gradle, you need to modify build.gradle to apply the war plugin to the project:

apply plugin: 'war'

The final step in the process is to ensure that the embedded servlet container doesn’t interfere with the servlet container to which the war file will be deployed. To do so, you need to mark the embedded servlet container dependency as provided.

dependencies { // … providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' // … }

关于spring - 如何在tomcat 8下运行Spring RESTful项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39305293/

相关文章:

java - 来自 aws-java-sdk 的 DynamoDB 在 gradle 下不完整

java - 如何向 intelliJ 自动完成添加新的检测方法?

java - java类JAVA中的Spring xml配置重写

spring - HttpSessionListener 实现内部的依赖注入(inject)

java - 如何利用 Spring 3 的 SSO 安全性?

java - IntelliJ IDEA嵌入式终端输出编码问题

build - 如何将 build.gradle 目录传递给 gradlew?

gradle - 我如何在 Spock 中运行一个特定的规范?与 GEB 和 gradle 一起使用

java - Gradle 下载不需要的依赖项

java - spring mvc herbinate 与额外列的多对多关系存在问题