Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run spring boot app in Eclipse tomcat?

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.

like image 272
blue-sky Avatar asked Jan 21 '16 15:01

blue-sky


2 Answers

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!

like image 113
leo Avatar answered Oct 10 '22 19:10

leo


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.

like image 24
andy Avatar answered Oct 10 '22 19:10

andy