2016-10-08 12 views
-1

我有一個使用spring數據jpa和hibernate(Services,Entities,Repositories,...)的maven項目。所有東西都用AspectJ編譯成一個jar文件。如何在沒有戰爭部署的情況下使用Spring Boot與現有的Tomcat

現在我想添加一個REST API以接收POST/GET消息。所以我添加了spring boot和spring web以將RequestMappings ans Controller添加到我的項目中。

問題是,我可以不是修改該項目的main(String[] args)方法,所以我不能添加Spring引導上下文,以便在Tomcat中使用構建。

因此,我想知道是否可以使用安裝在我的jar文件將被執行的機器上的tomcat。

我在StackOverflow上找到的唯一方法是編譯戰爭並將其部署到Tomcat上。但我的項目架構非常有限。所以事實上,我有一個必須位於特定文件夾中的jar文件,而且我的限制不會修改main(String[] args)方法,這有點煩人。那麼我能不知何故告訴spring使用現有的tomcat並收聽傳入連接?或者還有其他方法可以將一個可用的REST API應用到這種類型的項目結構中嗎?

回答

0

從遷移到JAR WAR:

  • 變化的pom.xml包裝:

<packaging>war</packaging>

  • 請確保您有這些依賴完全一樣:

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>

  • 是SpringApplication.run調用同一路徑上創建ServletInitializer準確。

    package com.example; 
    
    import org.springframework.boot.builder.SpringApplicationBuilder; 
    import org.springframework.boot.web.support.SpringBootServletInitializer; 
    
    public class ServletInitializer extends SpringBootServletInitializer { 
    
        @Override 
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
         return application.sources(DemoApplication.class); 
        } 
    } 
    
  • PS:我還建議把上的pom.xml,裏面的 「」 的標籤,您的項目的背景下,以下參數:

<finalName>myProject</finalName>

來源: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging

+0

tomcat的應排除 –

+0

http://stackoverflow.com/a/6647178/4857050 –

1

如果你想使用現有的Tomcat實例你應該向它部署一個war文件。該實例正在運行它自己的JVM並掛接到它,您應該提供ServletInitializer

查看詳細說明如何到Deploying Spring Boot Applications. What about the Java EE Application Server?

但是,我想你想知道,「我怎麼把它部署到現有 Tomcat安裝,或經典的Java EE應用服務器 (其中一些花費了很多錢!)像WebSphere,WebLogic或者JBoss?「Easy!畢竟,它仍然只是春天,所以 是必需的。您需要做出三個直觀的更改:從 jar版本移植到Maven的war build:在pom.xml文件中註釋 spring-boot-maven-plugin插件的聲明,然後更改 Maven包裝類型打仗。最後,在您的 應用程序中添加一個Web入口點。


您應該進一步讀取這個文件,你可以更改pom.xml排除未使用的依賴。

如果您的類與作爲較大應用程序服務器參與者的 發生衝突,則可能會遇到問題。在這種情況下,使用 您的構建工具的工具來排除或製作相關API的 。


相關問題