SpringBoot Swagger 配置动态host 以及token

package net.ameizi.config;

import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {

    @Value("${swagger.host}")
    private String swaggerHost;
    ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("amz")
                .description("基于shiro和jwt的前后端分离权限系统")
                .termsOfServiceUrl("")
                .version("1.0.0")
                .contact(new Contact("", "", ""))
                .build();
    }

    @Bean
    public Docket createRestApi() {
        ParameterBuilder builder = new ParameterBuilder();
        Parameter parameter = builder
                // 从cookie中获取token
                .parameterType("cookie") //参数类型支持header, cookie, body, query etc
                .name("token") //参数名
                .defaultValue("") //默认值
                .description("请输入token")
                .modelRef(new ModelRef("string")) //指定参数值的类型
                .required(false).build(); //非必需,这里是全局配置,然而在登陆的时候是不用验证的
        List<Parameter> parameters = Lists.newArrayList(parameter);
        return new Docket(DocumentationType.SWAGGER_2)
                .host(this.swaggerHost)
                .select()
                .apis(RequestHandlerSelectors.basePackage("net.ameizi"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(this.apiInfo())
                .globalOperationParameters(parameters);
    }

    /**
     * swagger ui资源映射
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars*")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * swagger-ui.html路径映射,浏览器中使用/api-docs访问
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/api-docs","/swagger-ui.html");
    }
}
swagger:
  host: localhost:8080
Spring Boot Swagger(现在更常用的是Springfox)是一个用于文档生成的强大工具,它允许你在Spring应用程序中轻松地添加API文档。配置Servers主要是为了指定API的上下文路径以及基础URL,以便生成的文档能够准确反映应用的实际部署位置。 在Swagger配置中,你可以通过`@EnableSwagger2`注解启用Swagger,并在`application.yml`或`application.properties`文件中设置相关的服务器信息。以下是基本的配置步骤: 1. 添加依赖:在你的pom.xml或build.gradle文件中加入Springfox的依赖。 ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.x.x</version> <!-- 使用最新的版本 --> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.x.x</version> </dependency> ``` 2. 配置application.yml(YAML格式): ```yaml swagger: servers: - url: http://localhost:8080/api-docs # 本地开发环境 description: "本地开发服务器" - url: https://your-api.example.com/docs # 生产环境 description: "生产环境API地址" ``` 或者application.properties (properties格式): ```properties swagger.servers.url=http://localhost:8080/api-docs swagger.servers.url=https://your-api.example.com/docs swagger.servers.description=生产环境API地址 ``` 在这里,`url`字段指定了API的基础URL,`description`则是描述性的名称,有助于用户理解每个服务器的作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值