简体   繁体   English

带有Gradle的Tomcat上的Spring REST服务

[英]Spring REST Service on Tomcat with Gradle

I've created a REST service with Spring and am having trouble understanding how I get this deployed to a Tomcat server. 我已经使用Spring创建了REST服务,但在理解如何将其部署到Tomcat服务器时遇到了麻烦。

I'm familiar with putting applications on a Tomcat instance with servlet mappings and such but it appears that this is all handled in the Spring annotations within the Java classes. 我熟悉将应用程序放置在具有servlet映射等的Tomcat实例上,但似乎所有这些都在Java类的Spring批注中处理。 I've got the 'war' plugin enabled in Gradle and everything seems to export fine. 我已经在Gradle中启用了'war'插件,并且一切似乎都可以正常导出。 I'm just confused because Spring requires an Application class to run the controllers so how do I get this to work with something like the web.xml file? 我只是感到困惑,因为Spring需要一个Application类来运行控制器,所以如何使它与诸如web.xml文件之类的东西一起工作?

I may not even be on the right track. 我什至可能走错了轨道。 I also read something about deploying the .jar as the REST endpoint but I don't know where to begin there. 我还阅读了一些有关将.jar部署为REST端点的信息,但我不知道从哪里开始。

If someone could just give me a high-level overview of what needs to be done to get a Spring REST service such as the one built here to run on a Tomcat instance, I'd greatly appreciate it. 如果有人可以只给我需要的东西,如一个内置要做得到一个弹簧安置服务的高层次概述这里对一个Tomcat实例运行,我会非常感激。

Java 8 Spring Gradle 2.2.1 Windows 7 Java 8 Spring Gradle 2.2.1 Windows 7

You need to create an app (war package) with Spring servlet and spring context (there are looooots of tutorials in the internet). 您需要使用Spring servlet和spring上下文(互联网上有很多教程)来创建一个应用程序(war包)。 You need to include component scanning (refer to the article here http://www.mkyong.com/spring/spring-auto-scanning-components/ for an example). 您需要包括组件扫描(例如,请参见http://www.mkyong.com/spring/spring-auto-scanning-components/上的文章)。

Spring scans components (and your controller) and recognizes @Controller or @RestController annotation and automatically assigns it to the routing. Spring扫描组件(和您的控制器)并识别@Controller或@RestController注释,并将其自动分配给路由。 And that's it :). 就是这样:)。

Since it sounds like you are using Spring Boot, you should only need to mark the Tomcat dependency as providedRuntime , as described at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-gradle-packaging : 因为它听起来就像你正在使用Spring启动,你应该只需要标记Tomcat的依赖性, providedRuntime ,如在描述http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-工具插件等级包装

...
apply plugin: 'war'

war {
    baseName = 'myapp'
    version =  '0.5.0'
}

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

configurations {
    providedRuntime
}

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

Then your war should be deployable to an external Tomcat instance. 然后,您的战争应该可以部署到外部Tomcat实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM