【问题标题】:IntelliJ with spring-boot-jetty-jspIntelliJ 与 spring-boot-jetty-jsp
【发布时间】:2016-11-19 06:59:42
【问题描述】:

我正在尝试运行 spring-boot-jetty-jsp 示例。它使用mvn jetty:run 在命令行中工作,但是当我在 IntelliJ 中尝试时,我得到了这个错误:

2016-07-16 02:54:25.150  INFO 19012 --- [           main] s.jetty.jsp.SampleJettyJspApplication    : Starting SampleJettyJspApplication on dac-Latitude-E7450 with PID 19012 (/home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp/target/classes started by dac in /home/dac/proj/spring-boot-master/spring-boot-samples/spring-boot-sample-jetty-jsp)
2016-07-16 02:54:25.153  INFO 19012 --- [           main] s.jetty.jsp.SampleJettyJspApplication    : No active profile set, falling back to default profiles: default
2016-07-16 02:54:25.252  INFO 19012 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@3ad83a66: startup date [Sat Jul 16 02:54:25 CEST 2016]; root of context hierarchy
2016-07-16 02:54:25.402  WARN 19012 --- [           main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [sample.jetty.jsp.SampleJettyJspApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer
2016-07-16 02:54:25.426 ERROR 19012 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [sample.jetty.jsp.SampleJettyJspApplication]; nested exception is java.lang.IllegalStateException: Failed to introspect annotated methods on class org.springframework.boot.web.support.SpringBootServletInitializer
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:187) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:321) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:273) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:98) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:681) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:523) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:369) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:313) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1185) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1174) [spring-boot-1.4.0.BUILD-SNAPSHOT.jar:1.4.0.BUILD-SNAPSHOT]
    at sample.jetty.jsp.SampleJettyJspApplication.main(SampleJettyJspApplication.java:33) [classes/:na]
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.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:301) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:237) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:204) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:173) ~[spring-context-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    ... 12 common frames omitted
Caused by: java.lang.NoClassDefFoundError: javax/servlet/ServletContext
    at java.lang.Class.getDeclaredMethods0(Native Method) ~[na:1.8.0_91]
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) ~[na:1.8.0_91]
    at java.lang.Class.getDeclaredMethods(Class.java:1975) ~[na:1.8.0_91]
    at org.springframework.core.type.StandardAnnotationMetadata.getAnnotatedMethods(StandardAnnotationMetadata.java:152) ~[spring-core-4.3.2.BUILD-20160715.204924-12.jar:4.3.2.BUILD-SNAPSHOT]
    ... 16 common frames omitted
Caused by: java.lang.ClassNotFoundException: javax.servlet.ServletContext
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_91]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_91]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_91]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_91]
    ... 20 common frames omitted

Disconnected from the target VM, address: '127.0.0.1:36461', transport: 'socket'

Process finished with exit code 1

可以在 IntelliJ 中创建一个 maven 配置并使用该配置运行 jetty:run 参数,然后它就可以工作了。但是上面的错误是什么意思呢?

【问题讨论】:

    标签: java maven intellij-idea spring-boot jetty


    【解决方案1】:

    这是因为示例将 Jetty 启动器依赖项列为 provided,因此 IntelliJ 未将其包含在您的模块中,导致 ServletContext 类不可用,从而导致 NoClassDefFound 异常。

    pom.xml 文件中更改 spring-boot-starter-jetty 依赖项的定义:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
        <!--<scope>provided</scope>-->
    </dependency>
    
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>apache-jsp</artifactId>
        <!--<scope>provided</scope>-->
    </dependency>
    

    并在 IntelliJ 中刷新/重新导入 maven 项目。

    或者,如果您不想更改示例代码,您可以在 IntelliJ 中手动编辑模块设置中的依赖项 - 只需将所有提供的依赖项更改为 Compile,它应该会产生类似的结果。

    【讨论】:

    • 但是当我改变它时,我不能使用我的jsp的根/请求映射。为什么不?我只是想使用一些jsp。我还必须从 org.eclipse.jetty 中注释掉 provided,然后我的根请求映射工作并且我的 jsp 呈现。
    • 是的,对不起,apache-jsp 也应该是编译依赖,我会更新答案,以便其他人也能看到。该示例的问题在于它使用war 类型的打包,这意味着它应该可以部署到服务器上。如果这些依赖项未设置为 provided,您将无法正确部署它。
    猜你喜欢
    • 2016-11-19
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 2023-04-08
    • 2014-06-17
    • 1970-01-01
    • 2016-10-16
    相关资源
    最近更新 更多