在 Spring Boot 中配置 Swagger UI

问题描述 投票:0回答:1

我在以下链接中遇到有关打开 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
spring spring-boot swagger swagger-ui
1个回答
0
投票

使用 springdoc 代替。
以下链接https://www.baeldung.com/spring-rest-openapi-documentation将为您提供帮助。

© www.soinside.com 2019 - 2024. All rights reserved.