8
votes

I have a Spring boot application and want to run this as a server app within Eclipse. So the app will be recognized as a Tomcat web app and can be added I update the facet :

enter image description here

When I run the web app my rest services are not being found. A spring boot app contains a different folder structure than spring apps before spring boot was released. Can a spring boot app be run from an eclipse configured tomcat ? Also prior to spring boot the controllers section of the app needs to specified. Do I need to update my spring boot app with these settings in order to run from Eclipse tomcat ?

I don't want to create a war and then copy it to webapps folder of tomcat.

Update : The reason to run this app within Eclipse tomcat container is that I have other non spring boot applications that my new spring boot app will depend on.

5
Just run the app you don't need to deploy it. - M. Deinum
@M.Deinum please see update - blue-sky
And why would you need to deploy it to tomcat for... Assuming you are using http to communicate that doesn't matter if it is the same tomcat or not. If you want to deploy to tomcat you need a war (this is explained in the reference guide). - M. Deinum
@M. Deinum making it a war will not enable running the app from Eclipse tomcat. Making a war will just allow the app to be run from tomcat standalone - blue-sky
@M.Deinum thanks for the guides, I'm not concerned with packaging the app , im just concerned with running spring and spring boot applications within same tomcat Eclipse container. Eclipse tomcat will not recognize a spring boot application as a web app without major changes to it's structure. - blue-sky

5 Answers

20
votes

Check your pom.xml:

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

Then check your main app class:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

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

Then configure Project Facets (as you have already did):

  • Dynamic Web Module
  • Java
  • Javascript

Then check your Deployment Assembly: enter image description here

Done!

2
votes

Short answer

  • use gradle plugins: eclipse-wtp and war
  • extend SpringBootServletInitializer in Application

Explanation:

In order to run a spring boot application on tomcat generally the produced artifact has to be a war application.

In order to make it run on tomcat inside Eclipse you have to use a WTP-Project (Web Tools Platform aka Dynamic Web Module) using the eclipse-wtp gradle plugin.

The SpringBootServletInitializer thing has to be extended in order to make Spring Boot application context launch when Tomcat is launched.

How to create a test project

Spring offers a Getting Started Tutorial how to build a REST webservice with Spring Boot. I enhanced the project to work with Tomcat and Eclipse:

Check out

the sample project:

git clone https://github.com/spring-guides/gs-rest-service.git

Import it into eclipse

Import the complete/ subfolder as gradle project.

Change build.gradle

remove

apply plugin: 'java'
apply plugin: 'eclipse'

add

apply plugin: 'war'
apply plugin: 'eclipse-wtp'

Change src/main/java/hello/Application.java

add extension of SpringBootServletInitializer

public class Application extends SpringBootServletInitializer {
    ...
}

That done I could deploy the sample project to a Tomcat server configured in eclipse and start the server. The url is: http://localhost:8080/complete/greeting?name=User

Check out the Complete Sample.

0
votes

For those who are unable to see "Deployment Assembly" option, adding below nature to the .project file will enable the deployment assembly option.

org.eclipse.wst.common.modulecore.ModuleCoreNature

After defining the packing structure for my project, I am able to run the application on Tomcat server without copying the war file in webapps directory manually.

-1
votes

Just add this annotation in the main class of spring boot.

@SpringBootApplication
@Configuration
@ComponentScan(value={"packages name to scan"})

Add dependence in pom.xml file.

-1
votes

Follow this 2 simple steps and your good to go!

Step 1. Change packaging format to war instead of jar in your pom.xml file.

     **<packaging>war</packaging>**

Step 2. Right click on your project then do Maven -> update project.

After doing this steps you will find an option to "Run on server" when you try to run your project.