简体   繁体   中英

How to use Spring Boot with existing Tomcat without war deployment

I have a maven project that uses spring data jpa and hibernate (Services, Entities, Repositories, ...). Everything is compiled to a jar file with AspectJ.

Now I wanted to add a REST API in order to receive POST/GET messages. So I added spring boot and spring web to add RequestMappings ans Controller to my project.

The problem is, that I can not modify the main(String[] args) method of the project so I can not add the Spring boot context in order to use the build in Tomcat.

Because of that I am wondering if I could use a tomcat that is installed on the machine where my jar file will be executed.

The only way that I found on StackOverflow is to compile a war and deploy it on the Tomcat. But I am very limited in my project architecture. So the fact that I have a jar file that must be located in a specific folder and also my restriction not to modify the main(String[] args) method is a little bit annoying. So can I somehow tell spring to use the existing tomcat and listen to the incoming connections? Or is there an other way to get a working REST API to this kind of project structure?

If you want to use existing Tomcat instance you should deploy a war file to it. The instance is running it's own JVM and to hook into it you should provide ServletInitializer .

See detailed description how to Deploying Spring Boot Applications. What about the Java EE Application Server? .

But, I imagine you wondering, “how do I deploy it to an existing Tomcat installation, or to the classic Java EE application servers (some of which cost a lot of money!) like WebSphere, WebLogic, or JBoss?” Easy! It's still just Spring, after all, so very little else is required. You'll need to make three intuitive changes: move from a jar build to a war build in Maven: comment out the declaration of the spring-boot-maven-plugin plugin in your pom.xml file, then change the Maven packaging type to war. Finally, add a web entry point into your application.


You should read this document further where you can make changes to pom.xml to exclude not used dependencies.

You may experience issues if you have classes that conflict with those that ship as parter of a larger application server. In this case, use your build tool's facilities for excluding or making optional the relevant APIs.


Migrate from JAR to WAR:

  • Change the packaging on pom.xml:

<packaging>war</packaging>

  • Make sure you have these dependencies exactly like that:

<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>

  • Create ServletInitializer exactly on the same path that SpringApplication.run is invoked.

     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: I also recommend putting on pom.xml, inside "" tag, the following parameter with the context of your project:

<finalName>myProject</finalName>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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