【问题标题】:Run Spring Boot with Eclipse Tomcat使用 Eclipse Tomcat 运行 Spring Boot
【发布时间】:2019-04-12 01:01:24
【问题描述】:

我正在尝试使用 Eclipse Tomcat 启动 Spring Boot,但它失败了。

我正在使用带有以下插件的 Eclipse

  • BuildShip Gradle 集成 2.0
  • Spring Tools 3 附加组件 3.9.6 发布

我使用 Spring Boot 创建了我的项目

  • 类型:gradle (BuildShip 2.x)
  • 包装:战争
  • Java 版本:8
  • 语言:Java

这是我的 build.gradle:

buildscript {
    ext {
        springBootVersion = '2.1.0.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}


apply plugin: 'java'
apply plugin: 'eclipse-wtp'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'war'

group = 'heykj.web'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

configurations {
    providedRuntime
}

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testImplementation('org.springframework.boot:spring-boot-starter-test')
    //providedCompile "javax.servlet:javax.servlet-api:4.0.1"
}

这是我的应用程序类:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class PlusFApplication extends SpringBootServletInitializer {

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

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

}

我的项目方面:

我不明白为什么这不起作用。我认为 Eclipse Tomcat 找不到主类。我添加了 servlet-api 但它不起作用。
我该如何解决?

对不起!有一些错字,我错过了我的情况。
首先,我知道 Spring Boot 嵌入了 Tomcat。
其次,我想从 Eclipse Tomcat 入手,而不是 Spring Boot 嵌入式 Tomcat。
谢谢!

2018 年 11 月 9 日
我试过这个但仍然失败..
build.gradle:

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
    }

    compileOnly group: 'javax.servlet', name: 'servlet-api', version: '3.0.1'

}

Tomcat 仍然找不到我的应用程序类。
我认为需要将 servlet-class 添加到 web.xml 中。

2018 年 11 月 9 日 - 2
build.gradle

dependencies {
    implementation('org.springframework.boot:spring-boot-starter-web') {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-tomcat'
        //compile group: 'org.springframework.boot', name: 'spring-boot-starter-tomcat', version: '2.1.0.RELEASE'
    }
    testImplementation('org.springframework.boot:spring-boot-starter-test')
    compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0'
}

完全排除 spring-boot-start-tomcat,我添加了 servleet-api。
现在他们给了我错误。

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.0.RELEASE)

2018-11-09 17:10:34.138  INFO 8712 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication on DESKTOP-E0BBPVE with PID 8712 (C:\Users\knowch\eclipse-workspace\demo\bin\main started by knowch in C:\Users\knowch\eclipse-workspace\demo)
2018-11-09 17:10:34.142  INFO 8712 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2018-11-09 17:10:34.796  WARN 8712 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
2018-11-09 17:10:34.805  INFO 8712 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-11-09 17:10:34.814 ERROR 8712 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at com.example.demo.DemoApplication.main(DemoApplication.java:17) [main/:na]
Caused by: org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.getWebServerFactory(ServletWebServerApplicationContext.java:204) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.createWebServer(ServletWebServerApplicationContext.java:178) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:152) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]

我的结果是他们必须找到嵌入的tomcat,这不是选项。
这是 TomcatWebServer.class

   /**
     * Create a new {@link TomcatWebServer} instance.
     * @param tomcat the underlying Tomcat server
     */
    public TomcatWebServer(Tomcat tomcat) {
        this(tomcat, true);
    }

    /**
     * Create a new {@link TomcatWebServer} instance.
     * @param tomcat the underlying Tomcat server
     * @param autoStart if the server should be started
     */
    public TomcatWebServer(Tomcat tomcat, boolean autoStart) {
        Assert.notNull(tomcat, "Tomcat Server must not be null");
        this.tomcat = tomcat;
        this.autoStart = autoStart;
        initialize();
    }

在IDE工具中,不能使用eclipse tomcat? :( 太伤心了..

【问题讨论】:

  • 您的应用程序运行情况如何,是否有任何错误?
  • Springboot 默认在 tomcat 服务器上运行,所以请您删除 tomcat 依赖并再次运行您的应用程序。
  • 你能分享一下错误吗?如果您能分享您所面临的确切错误,我会很乐意回答
  • @Arnaud 不,没有错误。也没有开始登录控制台。
  • @GauravRai1512 我会的。谢谢!

标签: java spring eclipse spring-boot


【解决方案1】:

Spring Boot 有一个内置的 Tomcat,它作为 Java 可执行文件运行。

只需右键单击 PlusFApplication 类,然后:

Run As -> Spring Boot App

或者

Run As -> Java Application

你的类应该是这样的,其余的由 Spring Boot 完成:

@SpringBootApplication
public class PlusFApplication {

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

如果您在导入 hello world example 时遇到问题,请先使其正常工作。

【讨论】:

  • 我没有这个选项Run -> Spring Boot App,怎么加?
  • @user7294900 您的项目设置不正确,最简单的方法是将源代码重新导入为 maven 项目。退出 eclipse,确保在再次导入之前清除所有旧的 eclipse 文件夹和文件(.project、.classpath、.settings)。
猜你喜欢
  • 2018-12-01
  • 2016-04-27
  • 2017-02-21
  • 2020-06-03
  • 2020-04-06
  • 1970-01-01
  • 2018-05-13
  • 2015-01-05
  • 2019-01-27
相关资源
最近更新 更多