我正在尝试使用SpringFox。
Spring版本:‘org.SpringFrawork.boot:3.0.0-快照’
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'
...
}
弹簧启动级
@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());
}
}
当我启动任务'bootRun‘时,我会得到一个错误:
[ 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
我尝试使用SpringFox演示项目示例和SpringFox,但每次都会遇到这个错误。
发布于 2022-03-21 07:08:22
SpringBoot3.0是为Java17和JakartaEE构建的,而不是 JavaEE。Spring 3还在开发中,还没有最终发布。目前还没有正式的发布日期,但预计不会比2022年的Q4更早发布。在此之前,我不会考虑Spring 3的生产(或者当他们开始发布候选版本时)。
尽管如此,目前还没有支持SpringFox的JakartaEE版本,因此无法在SpringFox 3中使用(参见这)。因此,在解决这个问题之前,SpringFox和SpringFox 3的结合是行不通的。
使用2.6.x (目前这是Spring的最新版本)。
https://stackoverflow.com/questions/71549614
复制相似问题