Using undertow with this project https://github.com/ddezoysa/keycloak-springboot-microservice from this tutorial Securing Spring Boot REST APIs with Keycloak is causing the error "No ServletContext set". With tomcat, as in the original project, everything is ok. But I need to use undertow. I debugged the project and the error is raised after going through all the project code.
Just change this in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>`
to this:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
</dependency>
And when it starts raises the "No ServletContext set" error:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.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`
Caused by: java.lang.IllegalStateException: No ServletContext set
at org.springframework.util.Assert.state(Assert.java:73) ~[spring-core-5.2.5.RELEASE.jar:5.2.5.RELEASE]
Thank You!
Comment From: bclozel
I believe this is a Keycloak issue, although I have no way to track down the actual change nor in which version it's been fixed. Please reach out to the project author or the keycloak Spring Boot adapter maintainers if you'd like to get this fixed in the project.
Thanks!