使用IntelliJ部署启用Spring Boot的嵌入式Tomcat应用程序

3

我有一个基于Spring Boot的应用程序,使用嵌入式Tomcat。当通过mvn spring-boot:run命令部署时没有问题,但是当尝试使用IntelliJ Spring Boot插件部署时出现问题。需要注意的是,我们修改了默认的pom文件,将应用程序转换为可以部署到完整Tomcat传统部署的war包,但在开发模式下,最好只使用嵌入式Tomcat来部署应用程序。问题在于,当我们尝试从IntelliJ运行Spring Boot应用程序时,基本上会出现以下错误消息。

Caused by: java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:163) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.retrieveBeanMethodMetadata(ConfigurationClassParser.java:380) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:314) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:245) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:198) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:167) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    ... 17 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_144]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_144]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_144]
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) ~[spring-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    ... 22 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_144]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_144]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335) ~[na:1.8.0_144]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_144]
    ... 26 common frames omitted

你有任何想法为什么会发生这种情况吗?我们该如何解决它?
编辑:
我正在使用 IntelliJ IDEA 2017.2(版本号 idea-IU-172.3317.76),作为一个SSCCE,让我们使用Spring Boot的Getting Started示例链接getting-started example of spring-boot,并应用传统部署所建议的更改(第一个链接),我们将得到以下文件。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.6.RELEASE</version>
    </parent>

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

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

然后我们将拥有Application.java
package hello;

import java.util.Arrays;

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

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }
    }

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

}

我并没有修改HelloController.java,只是为了完整性而提及:

package hello;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }

}

这个例子在使用 mvn spring-boot:run 命令时可以正常运行,但是在使用 spring-boot IntelliJ 插件时无法运行。


是的,但所有这些依赖项(或应该)由Tomcat提供,问题在于当我在IntelliJ中运行应用程序时,这些依赖项就不可用了,但是当我从控制台运行时它们是可用的,我不知道为什么 xD。 - carpinchosaurio
也许在这里?https://dev59.com/8Ok6XIcBkEYKwwoYEPzw - hoodaticus
被接受的解决方案与Gradle有关,而最受欢迎的答案再次依赖于普通的Maven任务,而不是来自IntelliJ的Spring Boot插件。但如果没有其他方法,我想我只能使用Maven方法并放弃IntelliJ插件。 - carpinchosaurio
你有在依赖列表中添加 spring-boot-starter-web 吗? - Morfic
是的,这实际上是我的第一个依赖项。 - carpinchosaurio
显示剩余5条评论
2个回答

4

注意 - 此答案引用了Spring文档v1.5.6.RELEASE(当前版本)。


分析:

从IntelliJ IDEA运行主类就像从命令行执行应用程序一样。因此,类路径将仅包括compileruntime作用域的依赖项。Provided表示在编译期间需要它,但你希望JDK或容器在运行时为你的应用程序提供它:

provided 这就像compile,但指示你期望JDK或容器在运行时提供该依赖项。例如,在为Java Enterprise Edition构建Web应用程序时,您会将Servlet API和相关的Java EE API的依赖关系设置为范围为provided,因为Web容器提供这些类。此范围仅在编译和测试类路径上可用,不是传递性的。

另一方面,spring-boot-maven-plugin使用不同的类路径来执行run目标:

默认情况下,repackagerun目标都将包含在项目中定义的任何provided依赖项。基于Spring Boot的项目应将provided依赖项视为运行应用程序所需的容器依赖项。


结论和解决方案:

因此,这是一个类路径配置问题。如你所提到的,对于war打包传统部署,你应该排除嵌入式servlet容器。然而,在开发期间运行应用程序时,你需要嵌入式容器。

作为解决方案或工作区,你至少有以下两个选项:

1)spring-boot-starter-tomcat依赖声明中删除<scope>provided</scope>。如果您愿意,您也可以将该依赖项完全删除,因为Tomcat是spring-boot-starter-web使用的默认容器,但最好还是保留它并添加注释,以免在发布时遗忘。 2) IJ中一个有用的技巧是,在生产发布之前不要忘记取消注释provided,方法是保留当前依赖关系,并添加一个Maven dev配置文件,您可以从IJ激活这个配置文件。这样,默认情况下它将是provided,可以安全地打包成war文件,只有在开发和手动激活配置文件时才包含它。添加配置文件后,请重新导入Maven项目,以便选择配置文件。
<profiles>
    <profile>
        <id>dev</id>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </dependency>
        </dependencies>
    </profile>
</profiles>

reimport project and activate profile


0

谢谢@Mayank Sharma,但是将其添加到pom.xml后仍然存在相同的问题。 - carpinchosaurio
删除.m2文件夹并重新更新项目,然后运行它。 - Anshul Sharma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接