简体   繁体   English

如何在Eclipse tomcat中运行spring boot app?

[英]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. 我有一个Spring启动应用程序,并希望在Eclipse中将其作为服务器应用程序运行。 So the app will be recognized as a Tomcat web app and can be added I update the facet : 因此,应用程序将被识别为Tomcat Web应用程序,并且可以添加我更新构面:

在此输入图像描述

When I run the web app my rest services are not being found. 当我运行Web应用程序时,我找不到其他服务。 A spring boot app contains a different folder structure than spring apps before spring boot was released. 在春季启动发布之前,Spring启动应用程序包含与spring应用程序不同的文件夹结构。 Can a spring boot app be run from an eclipse configured tomcat ? 可以从eclipse配置的tomcat运行spring boot应用程序吗? 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 ? 我是否需要使用这些设置更新我的spring启动应用程序才能从Eclipse tomcat运行?

I don't want to create a war and then copy it to webapps folder of tomcat. 我不想创建一个战争,然后将其复制到tomcat的webapps文件夹。

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. 更新:在Eclipse tomcat容器中运行此应用程序的原因是我有其他非Spring启动应用程序,我的新Spring启动应用程序将依赖它。

Check your pom.xml: 检查你的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: 然后检查您的主app类:

@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): 然后配置Project Facets(就像你已经做过的那样):

  • Dynamic Web Module 动态Web模块
  • Java Java的
  • Javascript 使用Javascript

Then check your Deployment Assembly: 然后检查部署程序集: 在此输入图像描述

Done! 完成!

Short answer 简短的回答

  • use gradle plugins: eclipse-wtp and war 使用gradle插件: eclipse-wtpwar
  • extend SpringBootServletInitializer in Application Application扩展SpringBootServletInitializer

Explanation: 说明:

In order to run a spring boot application on tomcat generally the produced artifact has to be a war application. 为了在tomcat上运行spring boot应用程序,通常生成的工件必须是war应用程序。

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. 为了使它在Eclipse中的tomcat上运行,你必须使用eclipse-wtp gradle插件使用WTP-Project(Web Tools Platform,即动态Web模块)。

The SpringBootServletInitializer thing has to be extended in order to make Spring Boot application context launch when Tomcat is launched. 必须扩展SpringBootServletInitializer ,以便在Tomcat启动时启动Spring Boot应用程序上下文。

How to create a test project 如何创建测试项目

Spring offers a Getting Started Tutorial how to build a REST webservice with Spring Boot. Spring提供了一个入门教程,介绍如何使用Spring Boot构建REST Web服务。 I enhanced the project to work with Tomcat and Eclipse: 我增强了项目以使用Tomcat和Eclipse:

Check out 查看

the sample project: 示例项目:

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

Import it into eclipse 将它导入eclipse

Import the complete/ subfolder as gradle project. complete/子文件夹导入为gradle项目。

Change build.gradle 更改build.gradle

remove 去掉

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

add

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

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

add extension of SpringBootServletInitializer 添加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. 这样做我可以将示例项目部署到在eclipse中配置的Tomcat服务器并启动服务器。 The url is: http://localhost:8080/complete/greeting?name=User 网址是: http:// localhost:8080 / complete / greeting?name = User

Check out the Complete Sample . 查看完整样本

For those who are unable to see "Deployment Assembly" option, adding below nature to the .project file will enable the deployment assembly option. 对于那些无法看到“部署程序集”选项的人,在.project文件中添加以下属性将启用部署程序集选项。

org.eclipse.wst.common.modulecore.ModuleCoreNature 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. 在为我的项目定义打包结构之后,我能够在Tomcat服务器上运行该应用程序,而无需手动复制webapps目录中的war文件。

Just add this annotation in the main class of spring boot. 只需在spring boot的主类中添加此注释即可。

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

Add dependence in pom.xml file. pom.xml文件中添加依赖项。

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. 步骤1.在pom.xml文件中将打包格式更改为war而不是jar。

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

Step 2. Right click on your project then do Maven -> update project. 步骤2.右键单击您的项目,然后执行Maven - >更新项目。

After doing this steps you will find an option to "Run on server" when you try to run your project. 执行此步骤后,您将在尝试运行项目时找到“在服务器上运行”选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM