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参数