Spring 5.0.0.M3调用ApplicationEventListener时出错:未设置ServletContext-上下文初始化期间遇到异常 [英] Spring 5.0.0.M3 Error calling ApplicationEventListener : No ServletContext set - Exception encountered during context initialization

查看:754
本文介绍了Spring 5.0.0.M3调用ApplicationEventListener时出错:未设置ServletContext-上下文初始化期间遇到异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我决定从最新的Spring 4.3官方版本切换到Spring 5.0.0.M3,这要归功于他对junit多线程内置功能的支持以及Flux gesture确实听起来很有趣.

Today I decided to switch from latest Spring 4.3 official release to spring 5.0.0.M3 thanks for his support with junit multithreading built-in possibilities and it's Flux gestion that definitely sounds really interesting.

花了整整一天的时间尝试弄清楚pom.xml为何不起作用后,我终于设法使其起作用了,至少我认为,mvn test命令运行良好,并为所有命令输出了BUILD SUCCESS我的数百个单元测试中.尽管该应用程序无法启动,但它给了我这个奇怪的错误:

After spending the whole day playing with my pom.xml trying to figure out why it wouldn't work I finally managed to make it work, at least I thought, the mvn test command runs fine and output a BUILD SUCCESS for all of my hundred of unit tests. Altough the application doesn't boot and it gives me this strange error :

017-08-29 18:22:08.196  INFO 19052 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-29 18:22:08.197  INFO 19052 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-29 18:22:09.099  INFO 19052 --- [           main] b.a.s.AuthenticationManagerConfiguration : 

Using default security password: b5c2ef80-ab3e-4bec-91ba-c5e29b086018

2017-08-29 18:22:09.509  INFO 19052 --- [           main] o.s.s.web.DefaultSecurityFilterChain     : Creating filter chain: NegatedRequestMatcher [requestMatcher=Ant [pattern='^/((authenticate)|(swagger-ui.html)|(webjars[/a-zA-Z.1-9-]{0,})){0,1}$']], [io.swagger.security.AuthenticationTokenProcessingFilter@7caf14a2, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@399f9da1, org.springframework.security.web.context.SecurityContextPersistenceFilter@63d7f239, org.springframework.security.web.header.HeaderWriterFilter@14cf6819, org.springframework.security.web.authentication.logout.LogoutFilter@59f79dd6, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@180857ea, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@9e1b62d, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@74258c18, org.springframework.security.web.session.SessionManagementFilter@627ba2cd, org.springframework.security.web.access.ExceptionTranslationFilter@1e3fc5d4]
2017-08-29 18:22:09.854  WARN 19052 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
2017-08-29 18:22:09.892  INFO 19052 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2017-08-29 18:22:09.959  WARN 19052 --- [           main] o.s.b.c.e.EventPublishingRunListener     : Error calling ApplicationEventListener

java.lang.ClassCastException: org.springframework.boot.context.event.ApplicationFailedEvent cannot be cast to org.springframework.boot.web.context.WebServerInitializedEvent
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:159) [spring-context-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) [spring-context-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.boot.context.event.EventPublishingRunListener.finished(EventPublishingRunListener.java:114) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at org.springframework.boot.SpringApplicationRunListeners.callFinishedListener(SpringApplicationRunListeners.java:79) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at org.springframework.boot.SpringApplicationRunListeners.finished(SpringApplicationRunListeners.java:72) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:803) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at io.swagger.Swagger2SpringBoot.main(Swagger2SpringBoot.java:44) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
	at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496) [spring-boot-maven-plugin-2.0.0.M3.jar:2.0.0.M3]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]

2017-08-29 18:22:09.990  INFO 19052 --- [           main] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-29 18:22:09.999 ERROR 19052 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1261) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1109) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:499) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) ~[spring-boot-2.0.0.M3.jar:2.0.0.M3]
	at io.swagger.Swagger2SpringBoot.main(Swagger2SpringBoot.java:44) [classes/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
	at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496) [spring-boot-maven-plugin-2.0.0.M3.jar:2.0.0.M3]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_101]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:182) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:575) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	... 22 common frames omitted
Caused by: java.lang.IllegalStateException: No ServletContext set
	at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:479) ~[spring-webmvc-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e96afb94.CGLIB$resourceHandlerMapping$31(<generated>) ~[spring-webmvc-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e96afb94$$FastClassBySpringCGLIB$$cb32715e.invoke(<generated>) ~[spring-webmvc-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:360) ~[spring-context-5.0.0.RC3.jar:5.0.0.RC3]
	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e96afb94.resourceHandlerMapping(<generated>) ~[spring-webmvc-5.0.0.RC3.jar:5.0.0.RC3]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_101]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_101]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_101]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_101]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:155) ~[spring-beans-5.0.0.RC3.jar:5.0.0.RC3]
	... 23 common frames omitted

[WARNING] 
java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:496)
	at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1261)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1109)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:499)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:312)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:868)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:327)
	at io.swagger.Swagger2SpringBoot.main(Swagger2SpringBoot.java:44)
	... 6 more
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:182)
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:575)
	... 22 more
Caused by: java.lang.IllegalStateException: No ServletContext set
	at org.springframework.util.Assert.state(Assert.java:73)
	at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:479)
	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e96afb94.CGLIB$resourceHandlerMapping$31(<generated>)
	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e96afb94$$FastClassBySpringCGLIB$$cb32715e.invoke(<generated>)
	at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
	at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:360)
	at org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration$$EnhancerBySpringCGLIB$$e96afb94.resourceHandlerMapping(<generated>)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:155)
	... 23 more
 [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.0.0.M3:run (default-cli) on project whimtrip-api: An exception occurred while running. null: InvocationTargetException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set -> [Help 1]
   

我在这里和在Google上搜索时发现了一些非常相似的错误,但问题总是来自另一个bean的错误,而该错误无法通过清晰的消息启动.就我而言,它表示未设置servlet上下文,我可以理解这是有问题的,但是我不明白为什么未设置它?有人知道吗?多谢您日后的协助!

I found some pretty similar errors down here and by searching on google but the thing is it always comes from an error with another bean that couldn't be initiated with a pretty clear message. In my case it says that the servlet context wasn't set which I can understand would be problematic, however I can't understand why it didn't setup? Any one has a clue? Thanks a bunch for your future help!

下面是我的pom.xml,这肯定是所有这些的原因:

below is my pom.xml which definitely might be the cause of all of this:

 <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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
  <groupId>****</groupId>
 <artifactId>**********</artifactId>
 <packaging>jar</packaging>
 <name>*****</name>
 <version>1.0.0</version>
 <properties>
    <java.version>1.8</java.version>
    <maven.compiler.source>${java.version}</maven.compiler.source>
    <maven.compiler.target>${java.version}</maven.compiler.target>
    <springfox-version>2.5.0</springfox-version>
    <spring-core-version>5.0.0.RC3</spring-core-version>
    <spring-modules-version>5.0.0.M3</spring-modules-version>
    <spring-boot-version>2.0.0.M3</spring-boot-version>
</properties>
<repositories>
    <repository>
        <id>com.springsource.repository.bundles.release</id>
        <name>SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases</name>
        <url>http://repository.springsource.com/maven/bundles/release</url>
    </repository>
    <repository>
        <id>com.springsource.repository.bundles.external</id>
        <name>SpringSource Enterprise Bundle Repository - External Bundle Releases</name>
        <url>http://repository.springsource.com/maven/bundles/external</url>
    </repository>
    <repository>
        <id>org.springframework.maven.release</id>
        <name>Spring Maven Release Repository</name>
        <url>http://repo.springsource.org/libs-release-local</url>
        <releases><enabled>true</enabled></releases>
        <snapshots><enabled>false</enabled></snapshots>
    </repository>
    <!-- For testing against latest Spring snapshots -->
    <repository>
        <id>org.springframework.maven.snapshot</id>
        <name>Spring Maven Snapshot Repository</name>
        <url>http://repo.springsource.org/libs-snapshot-local</url>
        <releases><enabled>false</enabled></releases>
        <snapshots><enabled>true</enabled></snapshots>
    </repository>
    <!-- For developing against latest Spring milestones -->
    <repository>
        <id>org.springframework.maven.milestone</id>
        <name>Spring Maven Milestone Repository</name>
        <url>http://repo.springsource.org/libs-milestone-local</url>
        <snapshots><enabled>false</enabled></snapshots>
    </repository>
   <repository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>central</id>
        <name>milestone</name>
        <url>https://repo.spring.io/milestone</url>
    </repository>

    <repository>
        <id>mvnrepo2</id>
        <name>mvnrepo2</name>
        <layout>default</layout>
        <url>http://central.maven.org/maven2</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>


<pluginRepositories>
    <pluginRepository>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
        <id>central-spring</id>
        <name>milestone</name>
        <url>https://repo.spring.io/milestone</url>
    </pluginRepository>
</pluginRepositories>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <!--<version>1.3.5.RELEASE</version>-->
    <version>2.0.0.M3</version>
</parent>
<build>
    <sourceDirectory>src/main/java</sourceDirectory>


    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

        <plugin>
            <groupId>org.avaje.ebeanorm</groupId>
            <artifactId>avaje-ebeanorm-mavenenhancer</artifactId>
            <version>4.7.1</version>
            <executions>
                <execution>
                    <id>main</id>
                    <phase>process-classes</phase>
                    <configuration>
                        <packages>api.whimtrip.model.**</packages>
                        <transformArgs>debug=2</transformArgs>
                    </configuration>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>




    </plugins>

</build>
<dependencies>

    <!-- Spring libs -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>${spring-boot-version}</version>
    </dependency>


    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <!--<version>4.3.8.RELEASE</version>-->
        <version>${spring-core-version}</version>
    </dependency>


    <!--SpringFox dependencies -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox-version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox-version}</version>
    </dependency>

    <!-- Convert objects to and from xml and json  -->
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-joda</artifactId>
    </dependency>


    <!-- Date manipulation lib -->
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
    </dependency>


    <!-- War building dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>


    <!-- Bean Validation API support -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <!-- Ebean dependencies-->

    <dependency>
        <groupId>org.avaje.ebeanorm</groupId>
        <artifactId>avaje-ebeanorm</artifactId>
        <version>6.18.1</version>
    </dependency>

    <dependency>
        <groupId>org.avaje</groupId>
        <artifactId>ebean-spring</artifactId>
        <version>2.8.1</version>
    </dependency>




    <!--  Security Bundles  -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <!--<version>4.2.3.RELEASE</version>-->
        <version>${spring-modules-version}</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <!--<version>4.2.2.RELEASE</version>-->
        <version>${spring-modules-version}</version>
    </dependency>


    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <!--<version>4.2.2.RELEASE</version>-->
        <version>${spring-modules-version}</version>
    </dependency>



    <!-- Mail dependencies -->

    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-mail</artifactId>
        <version>3.0.2.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1-rev-1</version>
    </dependency>

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.3</version>
    </dependency>



    <!-- Tests libs -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <!--<version>4.3.8.RELEASE</version>-->
        <version>${spring-modules-version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>


    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>1.9.5</version>
        <scope>test</scope>
    </dependency>

</dependencies>


</project>

PS:很抱歉将日志作为javascript代码段放置,但我无法弄清楚如何在此处打印整个堆栈跟踪信息,除非它说我必须在每行前面添加4个空格.

PS: sorry for putting the log as a javascript snippet but I couldn't figure out how to print the whole stack trace in here without it saying that I have to add 4 spaces in front of each line.

推荐答案

在尝试共享一个不起作用的示例时,我发现此示例按预期工作.因此,我试图注释掉我的配置类的某些部分,并设法进行了弹簧引导.实际上,问题是以下原因: 我的Spring Configuration类扩展了WebMvcConfigurationSupport类,其中包含

While trying to share a non working example, I figured out that this one worked as expected. I therefore tried to comment some parts of my config classes and managed to make the spring boot. Actually the problem was the following : My Spring Configuration class which extended WebMvcConfigurationSupport class contained

public void addInterceptors(InterceptorRegistry registry);

出于某些自定义原因,我使用此方法添加了customHandlerInterceptor. 事实是,此拦截器是在另一个conf类中声明的bean,而我以前使用@Autowired批注在此实际类中将其获取.出于某种原因,我忽略了该功能,该功能以前可以在以前的Spring版本中使用,但现在不再可用了.通过在此类中声明了这个豆给我带来了所有这些问题,我设法使该豆不同于@Autowired批注.引导现在运行正常,但在每个请求下我都会收到404错误: 例如

I used this method to add a customHandlerInterceptor for some custom reasons. The thing is that this interceptor was a bean declared in another conf class and I used to get it in this actual class using the @Autowired annotation. For a reason I ignore, this used to work with previous Spring versions but now it doesn't anymore... I managed to get this bean differently than from @Autowired annotation by declaring this bean in this class that gave me all those problems. The boot is now running fine except at each request I get a 404 error : for example

2017-08-30 16:43:03.379 WARN 18584 --- [nio-8082-exec-4] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/v1/admin/validate] in DispatcherServlet with name 'dispatcherServlet'

编辑-

该映射实际上有效,但是在Spring 4.3中,它以前在每个端点名称之前都考虑了"/v1",该参数是在application.properties中给出的,而我认为并未考虑在内.似乎新的春季版本尚不支持swagger文档插件,因为整个文档只是不加载,而是在重定向时发送404. 除此之外,我的处理程序现在还没有正确注册,但是我想这是因为我对最新的修改做错了,所以我需要先解决这个问题.

The mapping actually works but in Spring 4.3 it used to take into account "/v1" before each endpoint's name, this parameter was given in the application.properties and it's not taken into account I suppose. It seems like swagger documentation plugin isn't supported yet with new spring version because the whole documentation just doesn't load and sends a 404 instead on redirection. Other than that, my handler isn't correctly registered now but I guess it's because I've done something wrong with my latest modification so I will need to fix this first.

编辑2-

我已经读到某个地方,在Spring 5中,您必须用"extends WebMvcConfigurerSupport"类替换"extends WebMvcConfigurerAdapter"类,因为不赞成使用第一个类,但是这对我不起作用,我必须实现WebMvcConfigurer.现在一切都按预期工作(包括摇摇欲坠).另外,要修改contextPath,您必须在application.properties中修改此参数:"server.servlet.context-path",而不是最后一个受支持的"server.contextPath"

I've read somewhere that in Spring 5 you have to replace the "extends WebMvcConfigurerAdapter" by "extends WebMvcConfigurerSupport" class because the first one was deprecated, however this didn't work for me and I had to implements WebMvcConfigurer instead. Now everything is working fine as expected (including swagger). Also, to modify the contextPath, you have to modify this param in your application.properties : "server.servlet.context-path" instead of last one supported "server.contextPath"

这篇关于Spring 5.0.0.M3调用ApplicationEventListener时出错:未设置ServletContext-上下文初始化期间遇到异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆