我在以下链接中遇到有关打开 Swagger 界面的问题:http://localhost:8080/swagger-ui/index.html
目前,当我尝试访问它时,我收到 500 错误,我不知道如何解决它。到目前为止我已经尝试了几种解决方案,但没有成功。
Java:
package com.ishotit.api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@EnableJpaRepositories
@SpringBootApplication
public class ApiApplication {
public static void main(String[] args) {
SpringApplication.run(ApiApplication.class, args);
}
}
@Configuration(proxyBeanMethods = false)
@EnableWebMvc
class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedMethods("*").allowedHeaders("*");
}
}
@Configuration
class SpringFoxConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
application.properties
:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.datasource.url=jdbc:postgresql://localhost:5432/ishotit
spring.datasource.username=ishotit
spring.datasource.password=apipassword
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
springdoc.enable-native-support=true
使用 springdoc 代替。
以下链接https://www.baeldung.com/spring-rest-openapi-documentation将为您提供帮助。