Search code examples
javaspringspring-bootillegalstateexceptionspring-webflux

Spring boot webflux application won't start IllegalStateException


Here is my build.gradle

buildscript {
    ext {
        springBootVersion = '2.0.0.M2'
    }
    repositories {
        mavenCentral()
        jcenter()
        maven { url "https://repo.spring.io/snapshot" }
        maven { url "https://repo.spring.io/milestone" }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
    }
}

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

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
    maven { url "https://repo.spring.io/snapshot" }
    maven { url "https://repo.spring.io/milestone" }
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-security',
            'org.springframework.boot:spring-boot-starter-mail',
            'org.springframework.boot:spring-boot-starter-validation',
            'org.springframework.boot:spring-boot-starter-webflux',
            // support libs
            'commons-io:commons-io:2.5',
            'commons-codec:commons-codec:1.10',
            'org.apache.commons:commons-lang3:3.4',
            'org.apache.commons:commons-collections4:4.1',
            //validation
            'javax.validation:validation-api:1.1.0.Final'
    compileOnly 'org.springframework.boot:spring-boot-configuration-processor',
            'org.projectlombok:lombok'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

When I tried to run application it fails with java.lang.IllegalStateException: java.lang.NullPointerException
Stacktrace:

java.lang.IllegalStateException: java.lang.NullPointerException
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:152) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.refresh(ReactiveWebServerApplicationContext.java:52) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:386) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M2.jar:2.0.0.M2]
    at com.getcredit.bankscraper.Application.main(Application.java:10) [main/:na]
Caused by: java.lang.NullPointerException: null
    at org.springframework.boot.web.embedded.netty.NettyWebServer.stop(NettyWebServer.java:113) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
    at org.springframework.boot.web.reactive.context.ReactiveWebServerApplicationContext.stopAndReleaseReactiveWebServer(ReactiveWebServerApplicationContext.java:148) ~[spring-boot-2.0.0.M2.jar:2.0.0.M2]
    ... 7 common frames omitted

Project was generated via start.spring.io site.


Solution

  • After some tests, I've found out my problem. That was an NPE exception in init method of my bean annotated with @PostConstruct. You can reproduce this issue like this:

    @Component
    class Test {
    
        String str;
    
        @PostConstruct
        private void init() {
            System.out.println(str.length());
        }
    }
    

    I don't understand why spring throws so unclear exception.