How to deploy spring boot web application on tomcat server

后端 未结 5 935
清歌不尽
清歌不尽 2020-12-04 16:50

I have created spring boot web application, but I am unable to deploy spring boot web application WAR file on tomcat and I am able to run it as java application. How to run

相关标签:
5条回答
  • 2020-12-04 17:22

    Spring boot provides option to deploy the application as a traditional war file in servlet 3.x (without web.xml)supporting tomcat server.Please see spring boot documentation for this. I will brief what you need to do here.

    step 1 : modify pom.xml to change the packaging to war:(that you already did)

    <packaging>war</packaging>
    

    step 2 : change your dependency

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

    to

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

    step 3 :modify your war name (if you need to avoid the version details appended with the war name) in pom.xml under <build> tag.

    <build>
        <finalName>web-service</finalName>
    .....
    

    step 4 : run maven build to create war : clean install step 5 : deploy the generated war file web-service.war in tomcat and request url in browser http://<tomcat ip>:<tomcat port>/web-service/hello

    You should get Hello World.

    Note: Also you can remove redundant dependencies as @Ali Dehghani said.

    0 讨论(0)
  • 2020-12-04 17:28

    I was faced with this problem. Much of the above is good advice. My problem was to deploy on the Pivotal TC server initially.

    1. Make the packaging in the pom a WAR.

      <packaging>war</packaging>
      
    2. Add dependencies to the pom

      <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>
      
    3. I used an Application class to hold the main(). Main had configuration code so that EntityManager etc could be injected. This EntityManager used information from the ApplicationContext and persistence.xml files for persistence information. Worked fine under SpringBoot but not under Tomcat. In fact under Tomcat the Main() is not called. The Application class extends SpringBootServletInitializer.

    4. The following method is added to the Application class:

      protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          Application obj = new Application();
          @SuppressWarnings("resource")
          ConfigurableApplicationContext applicationContext = 
               new ClassPathXmlApplicationContext("META-INF/spring/applicationContext.xml");
           applicationContext.registerShutdownHook();
           applicationContext.getBeanFactory().autowireBeanProperties(
               obj, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
          return application.sources(Application.class);
      }
      

    Only the last line is required - the other code was held in main() before and was moved here to get injection of the EntityManager working.

    1. The annotations needed are:

      @EnableAutoConfiguration
      @ComponentScan
      //@SpringBootApplication will consist of both of these and @Configuration
      
    2. Some urls may now need the root context changing to include

      <artifactId>contextname</artifactId>.
      

    Hope that helps

    0 讨论(0)
  • 2020-12-04 17:29

    Here are two good documentations on how to deploy the Spring Boot App as a war file.

    You can follow this spring boot howto-traditional-deployment documentation -

    Steps according to this documentation -

    1. You update your application’s main class to extend SpringBootServletInitializer.

    2. The next step is to update your build configuration so that your project produces a war file rather than a jar file. <packaging>war</packaging>

    3. Mark the embedded servlet container dependency as provided.

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

    and one more way -

    See this spring io documentation which outlines how to deploy the spring boot app to an application server.

    Steps -

    1. Change jar packaging to war.

    2. Comment out the declaration of the spring-boot-maven-plugin plugin in your pom.xml

    3. Add a web entry point into your application by extending SpringBootServletInitializer and override the configure method

    4. Remove the spring-boot-starter-tomcat dependency and modfiy your spring-boot-starter-web dependency to

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    

    In your pom.xml, remove spring-beans and spring-webmvc dependencies. The spring-boot-starter-web dependency will include those dependecies.

    0 讨论(0)
  • 2020-12-04 17:30

    Mark the spring-boot-starter-tomcat dependency as provided, like:

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

    Note1: Remove redundant dependencies from your pom.xml like:

    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
    </dependency>
    

    They are part of spring boot starter packages

    Note2: Make jar not war

    0 讨论(0)
  • 2020-12-04 17:33

    The process of converting a spring boot jar to a spring boot war is documented at: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging Long story short, set your starter class the way you did in your example and then switch the packaging from jar to war in the .pom file. Furthermore, you need to set the spring-boot-starter-tomcat dependency to provided. Once again, the process is documented in it's complete form at the link above. Further information about this subject is available in the spring io guide, "Converting a Spring Boot JAR Application to a WAR" which is available at https://spring.io/guides/gs/convert-jar-to-war/ If i can be of any further assistance, let me know and i will help you.

    0 讨论(0)
提交回复
热议问题