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.
For the integration between spring-boot and swagger-ui, delete all Springfox related dependencies and just add the following library to the list of your project dependencies (No additional configuration is needed):
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.3.0</version>
</dependency>
The Swagger UI page will then be available at
http://localhost:8080/swagger-ui/index.html
The Swagger (currently known as OpenAPI) is a specification for visualizing web services. Springfox is an implementation of the specification which is no longer maintained by the owners and last commit of the project is in October 2020. At the time, javax
namespace was used by Spring framework, but newer versions use jakarta
.
Springfox still uses old version namespace (javax
), and of course it does not match with newer versions of Spring framework, so you have to:
I suggest you to use springdoc-openapi library instead. Base on the official documentation:
springdoc-openapi java library helps to automate the generation of API documentation using spring boot projects. springdoc-openapi works by examining an application at runtime to infer API semantics based on spring configurations, class structure and various annotations.
Notice: