首页 文章

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

提问于
浏览
5

我有一个Spring启动应用程序,并希望在Eclipse中将其作为服务器应用程序运行 . 因此,应用程序将被识别为Tomcat Web应用程序,并且可以添加我更新构面:

enter image description here

当我运行Web应用程序时,我找不到其他服务 . 在 Spring 季启动发布之前,Spring启动应用程序包含与spring应用程序不同的文件夹结构 . 可以从eclipse配置的tomcat运行spring boot应用程序吗?此外,在 Spring 季启动之前,应用程序的控制器部分需要指定 . 我是否需要使用这些设置更新我的spring启动应用程序才能从Eclipse tomcat运行?

我不想创建一个战争,然后将其复制到tomcat的webapps文件夹 .

更新:在Eclipse tomcat容器中运行此应用程序的原因是我有其他非Spring启动应用程序,我的新Spring启动应用程序将依赖它 .

5 回答

  • 0

    检查你的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>
    

    然后检查您的主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);
        }
    }
    

    然后配置Project Facets(就像你已经做过的那样):

    • 动态Web模块

    • Java

    • Javascript

    然后检查部署程序集:
    enter image description here

    完成!

  • 13

    对于那些无法看到“部署程序集”选项的人,在.project文件中添加以下属性将启用部署程序集选项 .

    org.eclipse.wst.common.modulecore.ModuleCoreNature

    在为我的项目定义打包结构之后,我能够在Tomcat服务器上运行该应用程序,而无需手动复制webapps目录中的war文件 .

  • 0

    只需在spring boot的主类中添加此注释即可 .

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

    pom.xml 文件中添加依赖项 .

  • 0

    简答

    • 使用gradle插件: eclipse-wtpwar

    • 延伸 SpringBootServletInitializer in Application

    说明:

    为了在tomcat上运行spring boot应用程序,通常生成的工件必须是 war 应用程序 .

    为了使它在Eclipse中的tomcat上运行,你必须使用 eclipse-wtp gradle插件使用WTP-Project(Web工具平台,即动态Web模块) .

    必须扩展 SpringBootServletInitializer 才能在启动Tomcat时启动Spring Boot应用程序上下文 .

    如何创建测试项目

    Spring提供了一个Getting Started Tutorial如何使用Spring Boot构建REST Web服务 . 我增强了项目以使用Tomcat和Eclipse:

    退房

    示例项目:

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

    将其导入eclipse

    complete/ 子文件夹导入为gradle项目 .

    更改build.gradle

    去掉

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

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

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

    添加 SpringBootServletInitializer 的扩展名

    public class Application extends SpringBootServletInitializer {
        ...
    }
    

    这样做我可以将示例项目部署到在eclipse中配置的Tomcat服务器并启动服务器 . 网址是:http://localhost:8080/complete/greeting?name=User

    看看Complete Sample .

  • 0

    按照这两个简单的步骤,你的好去!

    步骤1.在pom.xml文件中将打包格式更改为war而不是jar .

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

    步骤2.右键单击您的项目,然后执行Maven - >更新项目 .

    执行此步骤后,您将在尝试运行项目时找到“在服务器上运行”选项 .

相关问题