question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SpringDocWebFluxConfiguration throws NoClassDefFoundError: javax/servlet/ServletException

See original GitHub issue

Describe the bug

We’re trying to migrate JHipster to use Springdoc instead of Springfox.

https://github.com/jhipster/jhipster-bom/pull/556

In a Spring WebFlux application, I have the following dependencies:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-webflux-core</artifactId>
</dependency>
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-data-rest</artifactId>
</dependency>

When the app starts, I get the following error:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 
'openApiResource' defined in class path resource 
[org/springdoc/webflux/core/SpringDocWebFluxConfiguration.class]: Unsatisfied dependency expressed through 
method 'openApiResource' parameter 2; nested exception is 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'responseBuilder' 
defined in class path resource [org/springdoc/webflux/core/SpringDocWebFluxConfiguration.class]: Bean 
instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.springdoc.core.GenericResponseService]: Factory method 'responseBuilder' threw 
exception; nested exception is java.lang.NoClassDefFoundError: javax/servlet/ServletException

Another strange thing that happens with these two dependencies. A new webflux + MongoDB app fails with the following when running mvn verify:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate 
[org.springdoc.core.GenericResponseService]: Factory method 'responseBuilder' threw exception; nested 
exception is java.lang.NoClassDefFoundError: Could not initialize class 
org.springdoc.core.GenericResponseService

However, if I change replace both dependencies with springdoc-openapi-webflux-ui, all tests pass.

To Reproduce

Steps to reproduce the behavior:

  • What version of spring-boot you are using? 2.5.7
  • What modules and versions of springdoc-openapi are you using? 1.5.13
  • What is the actual and the expected result using OpenAPI Description (yml or json)?
  • Provide with a sample code (HelloController) or Test that reproduces the problem

I can provide steps to reproduce, but it’s kinda complicated. Here’s an attempt:

git clone -b springdocs git@github.com:mshima/jhipster-bom.git
cd jhipster-bom
./mvnw install -Dgpg.skip=true
cd ..
git clone -b skip_ci-doc git@github.com:mshima/generator-jhipster.git
cd generator-jhipster
npm link

Then, create a new JHipster project (for example, in your ~/Downloads directory):

mkdir webflux-mongodb
cd webflux-mongodb

Create a .yo-rc.json file in this directory with the following contents:

{
  "generator-jhipster": {
    "applicationType": "monolith",
    "reactive": true,
    "baseName": "sampleWebfluxMongodb",
    "packageName": "tech.jhipster.sample",
    "packageFolder": "tech/jhipster/sample",
    "authenticationType": "jwt",
    "cacheProvider": "no",
    "enableHibernateCache": false,
    "websocket": false,
    "databaseType": "mongodb",
    "devDatabaseType": "mongodb",
    "prodDatabaseType": "mongodb",
    "searchEngine": false,
    "buildTool": "maven",
    "enableTranslation": true,
    "nativeLanguage": "en",
    "languages": ["en", "fr"],
    "testFrameworks": ["gatling", "cypress"],
    "serverPort": "8080",
    "jhiPrefix": "jhi",
    "clientPackageManager": "npm",
    "clientFramework": "angularX",
    "creationTimestamp": 1596513172471,
    "jwtSecretKey": "ZjY4MTM4YjI5YzMwZjhjYjI2OTNkNTRjMWQ5Y2Q0Y2YwOWNmZTE2NzRmYzU3NTMwM2NjOTE3MTllOTM3MWRkMzcyYTljMjVmNmQ0Y2MxOTUzODc0MDhhMTlkMDIxMzI2YzQzZDM2ZDE3MmQ3NjVkODk3OTVmYzljYTQyZDNmMTQ=",
    "jhipsterVersion": "7.4.0",
    "skipClient": false,
    "skipServer": false,
    "skipUserManagement": false,
    "skipCheckLengthOfIdentifier": false,
    "skipFakeData": false,
    "entitySuffix": "",
    "dtoSuffix": "DTO",
    "blueprints": [],
    "otherModules": [],
    "pages": [],
    "devServerPort": 4200,
    "clientTheme": "none",
    "clientThemeVariant": "",
    "withAdminUi": true,
    "serviceDiscoveryType": false,
    "enableGradleEnterprise": false
  }
}

Then, run jhipster.

Once the app is finished, run ./mvnw verify to see the test failures.

To start the app:

docker-compose -f src/main/docker/mongodb.yml up -d
./mvnw

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
bnasslahsencommented, Dec 4, 2021

@mshima,

Exactly, you are right! When using groups, the OpenAPIService scope should be PROTOTYPE. This a sample code how it’s implemented in springdoc-openapi:

https://github.com/springdoc/springdoc-openapi/blob/cfe86b6554bd46e2e4b74f686d1e9837fee65f29/springdoc-openapi-common/src/main/java/org/springdoc/core/SpringdocBeanFactoryConfigurer.java#L86-L92

This is not working in your case, as you have declared in JHipsterSpringDocAutoConfiguration a conflicting condition:

  • @AutoConfigureBefore(SpringDocConfiguration.class) vs @Import(SpringDocConfiguration.class) . This causes the CacheOrGroupedOpenApiCondition not to be verified.

I believe the straight forward solution will be as follow (The less of springdoc-openapi Beans overriding/duplication):

package tech.jhipster.config.apidoc;

import io.swagger.v3.oas.models.OpenAPI;
import org.springdoc.core.SpringDocConfiguration;
import tech.jhipster.config.JHipsterProperties;

import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;

import static tech.jhipster.config.JHipsterConstants.SPRING_PROFILE_API_DOCS;

/**
 * OpenApi OpenAPI configuration.
 * <p>
 * Warning! When having a lot of REST endpoints, OpenApi can become a performance issue.
 * In that case, you can use the "no-api-docs" Spring profile, so that this bean is ignored.
 */
@Configuration
@ConditionalOnWebApplication
@ConditionalOnClass(OpenAPI.class)
@Profile(SPRING_PROFILE_API_DOCS)
@AutoConfigureBefore(SpringDocConfiguration.class)
@AutoConfigureAfter(JHipsterProperties.class)
@Import(JHipsterSpringDocGroupsConfiguration.class)
public class JHipsterSpringDocAutoConfiguration {}

As you can see, i have also not loaded JHipsterSpringDocWebFluxConfiguration or JHipsterSpringDocMvcConfiguration as the loading of MultipleOpenApiWebMvcResource and MultipleOpenApiWebFluxResource is already done by the sprindoc-openapi starter.

Of course, you can also remove springdoc.cache.disabled = true, from the generated application.yml.

Don’t hesitate to let me know, if these instructions help you solve the cache issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot java.lang.NoClassDefFoundError: javax/servlet ...
ClassNotFoundException : javax.servlet.Filter. I had to enable the below option in Intellij. Add dependencies with "provided" scope to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found