Swagger配置、以及开关、升级界面、增加全局token方法

1、配置swagger 生效

1:添加swagger2依赖

<!--swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <scope>provided </scope>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <scope>provided </scope>
</dependency>

2:在@configruation中配置swaggwer2,其中@swagger2注解必须在类上

注意:在多工程里面配置类想要被加载,需要依赖此工程后,在启动类加上@ComponentScan(basePackages = {"com.qiyun 包路径"},要不然不会加载到别的工程的配置类)

@Configuration
@EnableSwagger2 //swagger2注解
public class SwaggerConfig {
    @Bean
    public Docket webApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                .paths(Predicates.not(PathSelectors.regex("/admin/.*")))
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();

    }

    private ApiInfo webApiInfo(){

        return new ApiInfoBuilder()
                .title("网站-课程中心API文档")
                .description("本文档描述了课程中心微服务接口定义")
                .version("1.0")
                .contact(new Contact("java", "http://qiyun.com", "1225126766@qq.com"))
                .build();
    }
}

3:在类、方法、参数上加上此注解

 1、实体类中 示例

@ApiModel(value="收货地址表",description="收货地址信息")
@ApiModelProperty(value = "省ID",name ="provinceId" )

 2、controller接口中 示例

@RestController
@RequestMapping("/evaluation")
@Api(tags = {"微信小程序--测评报告"})
public class EvaluationController
    @PostMapping("/getEvaluationTemplateByOrderId")
    @ApiOperation(value = "根据订单id获取测试报告模板以及商品SKU详情")
    @ApiImplicitParams(
            @ApiImplicitParam(paramType = "query",name = "orderId",value = "订单id")
    )
    public ResponseResult<EvaluationRecordModel> getEvaluationTemplateByOrderId(HttpServletRequest httpServletRequest, String orderId) {
        if (StringUtils.isEmpty(orderId)) return ResultUtils.failure("参数orderId不能为空");
        EvaluationRecordModel evaluationRecordVo = evaluationTemplateService.getEvaluationByOrderId(orderId);
        return ResultUtils.success(evaluationRecordVo);
    }

 

4:访问地址:http://localhost:8001/swagger-ui.html,一定要记住后缀为 /swagger-ui.html 这个是写死的

2、swagger的关闭和 开启两种设置

第一种

.enable(true) // 开启swagger

return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).globalOperationParameters(parameters)
                .enable(true) // 开启swagger
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }

第二种

@Profile({"dev","test"})   //配置测试环境

@Configuration
@EnableSwagger2
@Profile({"dev","test"})
public class SwaggerConfig {

    @Bean
    public Docket createRestApi(){
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder()
                .name("token")
                .description("认证token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build());

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).globalOperationParameters(parameters)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("XHS API Doc")
                .description("This is a restful api document of XHS.")
                .version("1.0")
                .build();
    }

}

3、升级swagger 界面

1.依赖

		<!-- swagger -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
	  <!--<dependency>-->
	  <!--<groupId>io.springfox</groupId>-->
	  <!--<artifactId>springfox-swagger-ui</artifactId>-->
	  <!--<version>2.9.2</version>-->
	  <!--</dependency>-->
	  <dependency>
		  <groupId>com.github.xiaoymin</groupId>
		  <artifactId>swagger-bootstrap-ui</artifactId>
		  <version>1.9.3</version>
	  </dependency>

2、访问地址修改为:/swagger-ui.html  改为  doc.html

4、增加全局token 方法

    @Bean
    public Docket createRestApi(){
        List<Parameter> parameters = new ArrayList<>();
        parameters.add(new ParameterBuilder()
                .name("token")
                .description("认证token")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build());

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).globalOperationParameters(parameters)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }

效果展示

swagger中的每个调试接口都带有token参数

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值