首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Swagger2更改Swagger Ui的基本路径

在Spring Boot应用程序中使用Swagger2时,默认情况下,Swagger UI的基本路径是/swagger-ui.html

方法一:使用@EnableSwagger2注解的basePackage属性

在配置Swagger2的Java配置类上,使用@EnableSwagger2注解的basePackage属性来指定基本路径。

代码语言:javascript
复制
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;

@Configuration
@EnableSwagger2(basePackages = "com.example.demo")
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

方法二:使用@EnableWebMvc注解的addViewControllers方法

在Spring Boot应用程序的主类上,使用@EnableWebMvc注解的addViewControllers方法来添加一个新的视图控制器,将Swagger UI的基本路径更改为所需的路径。

代码语言:javascript
复制
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;

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

    @Bean
    public ViewControllerRegistry viewControllerRegistry() {
        ViewControllerRegistry registry = new ViewControllerRegistry();
        registry.addViewController("/custom-path/swagger-ui.html").setViewName("forward:/swagger-ui.html");
        return registry;
    }
}

在这个例子中,我们将Swagger UI的基本路径更改为/custom-path/swagger-ui.html

方法三:使用application.properties文件

application.properties文件中,添加以下配置来更改Swagger UI的基本路径:

代码语言:javascript
复制
springfox.documentation.swagger.v2.path=/custom-path/swagger-ui.html

在这个例子中,我们将Swagger UI的基本路径更改为/custom-path/swagger-ui.html

完成上述操作后,重新启动应用程序,然后访问http://localhost:8080/custom-path/swagger-ui.html即可看到Swagger UI界面。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

没有搜到相关的沙龙

领券