Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 spring boot application as web service on tomcat. I am using following code. If it is possible to run on tomcat plz help me using annotations without using web.xml and with using web.xml.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
       return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
       SpringApplication.run(Application.class, args);
    }

}

Following code for rest controller

@RestController
public class HelloWorld{

   @RequestMapping(value = "/hello", method = RequestMethod.GET)
   public ResponseEntity<String> get() {
       return new ResponseEntity<String>("Hello World", HttpStatus.OK);
   }
}

Following Pom.xml I am using

<groupId>org.springframework</groupId>
<artifactId>web-service</artifactId>
<version>0.1.0</version>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.RELEASE</version>
</parent>

<dependencies>
    <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>
        </dependency>

        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
        <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
    </dependency>

</dependencies>

<properties>
    <java.version>1.6</java.version>
</properties>


<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>
<packaging>war</packaging>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
133 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...