0

I have Kotlin pet-project with docker-compose file:

version: '3.1'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    command: java -jar ./testapp.jar
    ports:
      - "8080:8080"
    depends_on:
      - postgres
    environment:
      - SPRING_DATASOURCE_URL=jdbc:postgresql://172.25.1.3:5432/kotlin-app
      - SPRING_DATASOURCE_USERNAME=postgres
      - SPRING_DATASOURCE_PASSWORD=Tolstochok3000
    networks:
      app-network:
        ipv4_address: 172.25.1.2

  postgres:
    build:
      context: docker/db
      dockerfile: Dockerfile
    command: postgres
    environment:
      POSTGRES_DB: "kotlin-app"
      POSTGRES_USER: "postgres"
      POSTGRES_PASSWORD: "Tolstochok3000"
    ports:
      - "5432:5432"
    networks:
      app-network:
        ipv4_address: 172.25.1.3
    restart: always
networks:
  app-network:
    ipam:
      config:
        - subnet: 172.25.1.0/16

I tried docker-compose up, postgres started without problem, but app throw exception: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set

When I started application on localhost (without docker), application work correctly with db in docker.

Application dockerfile:

FROM gradle:jdk17 as builder
WORKDIR /test-kotlin-app
COPY src ./src
COPY build.gradle.kts ./build.gradle.kts
RUN gradle clean build

FROM openjdk:17-alpine as backend
WORKDIR /root
COPY --from=builder /test-kotlin-app/build/libs/* ./app
ENTRYPOINT ["java", "-jar", "/root/app"]

Application propeties:

  #DATABSE SETTINGS
  datasource:
    driver-class-name: org.postgresql.Driver
    url: ${SPRING_DATASOURCE_URL:jdbc:postgresql://localhost:5432/kotlin-app}
    username: ${SPRING_DATASOURCE_USERNAME:postgres}
    password: ${SPRING_DATASOURCE_PASSWORD:Tolstochok3000}
  #JPA SETTINGS
  jpa:
    show-sql: 'true'
    hibernate:
      ddl-auto: none
    properties:
      hibernate:
        dialect: org.hibernate.dialect.PostgreSQLDialect

I tried change docker-compose file application.yml (remove variables) and try remove network, but it didn't help

Error: java.lang.IllegalStateException: No ServletContext set stacktrace:

test_kotlin_app-app-1       | 08:01:35.023 [main] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Before shutdown stats (total=10, active=0, idle=10, waiting=0)
test_kotlin_app-app-1       | 08:01:35.029 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@2db15f70: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.030 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@6d2041d5: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.030 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@22daa83a: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.030 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@3599b284: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.030 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@42f55775: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.030 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@54a28df5: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.030 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@5668d8b3: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.030 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@2e9d5d68: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.031 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@46bbe931: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.031 [HikariPool-1 connection closer] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Closing connection org.postgresql.jdbc.PgConnection@6b60936: (connection evicted)
test_kotlin_app-app-1       | 08:01:35.032 [main] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - After shutdown stats (total=0, active=0, idle=0, waiting=0)
test_kotlin_app-app-1       | 08:01:35.032 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.
test_kotlin_app-app-1       | 08:01:35.035 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
test_kotlin_app-app-1       | org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'resourceHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/W
ebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.se
rvlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; nested exception is java.lang.IllegalStateException: No ServletContext set
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
test_kotlin_app-app-1       |   at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
test_kotlin_app-app-1       |   at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
test_kotlin_app-app-1       |   at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
test_kotlin_app-app-1       |   at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
test_kotlin_app-app-1       |   at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
test_kotlin_app-app-1       |   at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306)
test_kotlin_app-app-1       |   at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295)
test_kotlin_app-app-1       |   at ru.rsu.app.KotlinAppApplicationKt.main(KotlinAppApplication.kt:13)
test_kotlin_app-app-1       | Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.web.servlet.HandlerMapping]: Factory method 'resourceHandlerMapping' threw exception; ne
sted exception is java.lang.IllegalStateException: No ServletContext set
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
test_kotlin_app-app-1       |   ... 18 common frames omitted
test_kotlin_app-app-1       | Caused by: java.lang.IllegalStateException: No ServletContext set
test_kotlin_app-app-1       |   at org.springframework.util.Assert.state(Assert.java:76)
test_kotlin_app-app-1       |   at org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport.resourceHandlerMapping(WebMvcConfigurationSupport.java:591)
test_kotlin_app-app-1       |   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
test_kotlin_app-app-1       |   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)
test_kotlin_app-app-1       |   at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
test_kotlin_app-app-1       |   at java.base/java.lang.reflect.Method.invoke(Method.java:568)
test_kotlin_app-app-1       |   at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
test_kotlin_app-app-1       |   ... 19 common frames omitted
5
  • May be link on projcet can help: github.com/NikRomanov3000/test-kotlin-pet-app Commented Nov 9, 2022 at 22:34
  • unrelated but changing jdbc:postgresql://172.25.1.3:5432/kotlin-app to jdbc:postgresql://postgres:5432/kotlin-app and removing all that networking stuff would rule out some number of configuration errors. Commented Nov 9, 2022 at 23:03
  • I think there's insufficient information here to help. You've provided neither the full stack trace nor the Dockerfile from the springboot project, therefore it may not be possible to assist. Commented Nov 9, 2022 at 23:06
  • @Colton I tried remove all network block and change jdbc:postgresql://172.25.1.3:5432/kotlin-app to jdbc:postgresql://postgres:5432/kotlin-app but it didn't help. Commented Nov 10, 2022 at 8:06
  • @Colton Also thank you for the comment. I added stacktrace and app dockerfile to description Commented Nov 10, 2022 at 8:11

2 Answers 2

1

I resolve problem, when fixed build section in build.gradle.kts file. Remove block tasks.withType and add build section with default spring boot builder:

tasks.named<org.springframework.boot.gradle.tasks.bundling.BootJar>("bootJar") {
mainClass.set("con.example.app.KotlinAppApplicationKt")
archiveBaseName.set("testapp")
archiveFileName.set("testapp.jar") }
Sign up to request clarification or add additional context in comments.

1 Comment

This solution resolves same propblem for me. But I use just tasks.named("bootJar"), otherwise i get error.
0

Perhaps your DB container is not yet ready when the app is initialized. You may want to implement some waiting mechanism like this or like some of the solutions here.

1 Comment

I tried add condition : depends_on: postgres: condition: service_healthy But it doesn't work. Same error: No ServletContext set

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.