28

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>

3
  • If I export as jar, I can't deploy in remote tomcat and unable to call this application from browser know Commented Dec 29, 2015 at 17:30
  • why should you need a separate tomcat for access application from browser.The spring boot itself contain the embedded server for that Commented Dec 30, 2015 at 14:32
  • If you still need it as a war, see my answer Commented Dec 30, 2015 at 15:45

6 Answers 6

34

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I did the changes you have said, but it throwing the selection can't be run on server means it is not recognizing war.
OK. Here is another Spring Boot Traditional Deployment Documentation - docs.spring.io/spring-boot/docs/current/reference/html/… . You can try this approach!
is there a way to make parts of the POM conditional to the lifecycle?
what benefit of war instead of jar? please check this question: stackoverflow.com/questions/43303849/…
12

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.

4 Comments

I am getting selection cannot be run on any server.
you are running from eclipse?
Yeah. I am running in eclipse.
can you ping me your number
4

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

4 Comments

If I make as jar, I can't deploy on remote tomcat server so it is not possible to call from web browser.
You could install spring boot apps as a service in linux or i guess windows and mac. All i'm saying is next time put more thought on how you're deploying your java apps (or any app, for that matter). This port binding style of app deployment has many advantages and is more aligned with today's best practices
I know sometimes we had to go this way because of some crazy corporate culture
How to use application as service. in remote tomcat server WAR file option only thing is there
0

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.

Comments

0

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

Comments

0

**Steps to deploy spring boot webapp on tomcat server on local area network is as follows :-It works 100% please try it because it is implemented by myself.**strong text

  1. add the server.port = 8080 and server.address = (your pc IP address) in your project's application.properties file. Remember:- dont put ip address inside the brackets used here just to show the ip address inside the brackets.

  2. Then make the war file of spring boot app you can make it in eclipse and also in VS code whatever IDE you are using.

how to generate war file in vs code:-

In the maven folder open the maven folder you will find your project and click on the folder and there is option named clean click on clean it will clean your project's folder named as target and after this again click on the same project and below the clean you will find a option named install click on that it will freshly create your folder named as target below the src folder in your project and open this target folder you will find there two war files one is simple and other is extension having original,from these files you have to upload the war file other than the original file.

  1. Upload the war file on tomcat server webapp folder by running the tomcat in browser and go the manager app in tomcat where you will find the option to upload the war file.

  2. After this turn off your public firewall network from firewall and protection in windows security software in your system from the taskbar.

  3. Go to the other computer on the same LAN and access the app url through the http://IP_address:8080/yourwebsite_url
    your website url will be same as your uploded war file in tomcat server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.