17

I'm trying to use SpringFox.

Spring Boot version: 'org.springframework.boot:3.0.0-SNAPSHOT'

build.gradle

dependencies {
...
  implementation 'io.springfox:springfox-petstore:2.10.5'
  implementation "io.springfox:springfox-swagger2:3.0.0"
  implementation "io.springfox:springfox-oas:3.0.0"
  implementation 'io.springfox:springfox-swagger-ui:3.0.0'
...
}

Spring Boot Class

@SpringBootApplication
@EnableSwagger2
@EnableOpenApi
public class ServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceApplication.class, args);
    }
}

SwaggerUiWebMvcConfigurer

@Component
public class SwaggerUiWebMvcConfigurer implements WebMvcConfigurer {
    private final String baseUrl;

    public SwaggerUiWebMvcConfigurer(
        @Value("${springfox.documentation.swagger-ui.base-url:}") String baseUrl) {
        this.baseUrl = baseUrl;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String baseUrl = StringUtils.trimTrailingCharacter(this.baseUrl, '/');
        registry.
            addResourceHandler(baseUrl + "/swagger-ui/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
            .resourceChain(false);
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController(baseUrl + "/swagger-ui/")
            .setViewName("forward:" + baseUrl + "/swagger-ui/index.html");
    }
}

SwaggerConfig

@Configuration
public class SwaggerConfig {
    @Bean
    public Docket petApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .groupName("full-petstore-api")
            .apiInfo(apiInfo())
            .select()
            .paths(any())
            .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("API")
            .description("Service API")
            .termsOfServiceUrl("http://springfox.io")
            .contact(new Contact("springfox", "", ""))
            .license("Apache License Version 2.0")
            .licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
            .version("2.0")
            .build();
    }
}

SecurityConfig

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().anonymous().and().csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse());
    }
}

When I start task 'bootRun', I get an error:

[  restartedMain] o.s.boot.SpringApplication: Application run failed

java.lang.TypeNotPresentException: Type javax.servlet.http.HttpServletRequest not present
    at java.base/sun.reflect.generics.factory.CoreReflectionFactory.makeNamedType(CoreReflectionFactory.java:117) ~[na:na]
    at java.base/sun.reflect.generics.visitor.Reifier.visitClassTypeSignature(Reifier.java:125) ~[na:na]
...

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

Caused by: java.lang.ClassNotFoundException: javax.servlet.http.HttpServletRequest

I tried to use SpringFox demo projects examples and SpringFox-Boot, but I get this error every time.

Shadow4571
  • 382
  • 2
  • 4
  • 10
  • 3
    Spring BOot 3 is for JakartaEE **not** JavaEE. Use 2.6 not 3.0, afaik there is no SpringFox version available for Spring Boot 3 (mainly due to that still being developed and not becoming final for about 6 to 7 months). – M. Deinum Mar 20 '22 at 19:04
  • @M.Deinum, thanks a lot. While I looking for a solution, I found the indicated problem on GitHub: https://github.com/springfox/springfox/issues/3983 I couldn't find an indication of Spring version support, until you answer. – Shadow4571 Mar 20 '22 at 20:23

3 Answers3

27

Spring Boot 3.0 is built for Java17 and JakartaEE, not JavaEE. Spring Boot 3 is still under development and hasn't seen a final release yet. There has been no official release date yet, but expect nothing sooner than Q4 of 2022. Until then I wouldn't consider Spring Boot 3 for production (or maybe when they start releasing Release Candidates).

That being said, there is currently no SpringFox release that supports JakartaEE and therefore isn't usable with Spring Boot 3 (see this). So until that has been resolved the combination of SpringFox and Spring Boot 3 won't work.

Use 2.6.x (which at this moment is the latest release version of Spring Boot).

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
14

Your Spring-Boot version is 3 which uses jakarta namespace instead of javax.

But Swagger still using old version namespace (javax).

They are not match, so you have to downgrade your Spring-Boot version to older one (2.7) or wait for new version of Swagger.


I suggest you to use springdoc-openapi instead. Compatible version of OpenApi for Spring-Boot 3 is these dependencies:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
        <version>2.0.2</version>
    </dependency>

The Swagger UI page will then be available at

http://localhost:8080/swagger-ui/index.html

P.S. If you have context-path then (http://server:port/context-path/swagger-ui.html)


For spring-boot v3 support, make sure you use springdoc-openapi v2

https://springdoc.org/v2/

Mehdi Rahimi
  • 1,453
  • 5
  • 20
  • 31
6

Just add this dependency into the pom.xml

https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api

        <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
            <scope>provided</scope>
        </dependency>

if necessary, also add this property into your application.properties

spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER

source: Failed to start bean 'documentationPluginsBootstrapper' in spring data rest

Kevkidev
  • 69
  • 1
  • 1