前言:
前段时间自己单建了个SpringBoot小demo 然后准备加入相关的条条框框,我们今天来看下我们整合swagger,保留下笔记以后方便自己查看。
回顾:
之前写的ssm整合swagger 连接:ssm整合swagger地址
一,swagger介绍
使用Swagger,就是把相关的信息存储在它定义的描述文件里面(yml或json格式),再通过维护这个描述文件可以去更新接口文档,以及生成各端代码。而Springfox-swagger,则可以通过扫描代码去生成这个描述文件,连描述文件都不需要再去维护了。所有的信息,都在代码里面了。代码即接口文档,接口文档即代码。
二,引入相关pom依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
三,相关配置类
/**
* @Description: swagger congfig 配置类
* @author: xxk
* @Date: 2019/12/9 19:45
* @ModifiedDate:
* @Copyright:保险股份有限公司
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig implements EnvironmentAware {
private final Logger logger = LoggerFactory.getLogger(SwaggerConfig.class);
private static final String DEFAULT_INCLUDE_PATTERN = "/.*";
private RelaxedPropertyResolver propertyResolver;
@Override
public void setEnvironment(Environment environment) {
this.propertyResolver = new RelaxedPropertyResolver(environment, "swagger.");
}
@Bean
public Docket swaggerSpringfoxDocket() {
logger.debug("Starting Swagger");
StopWatch watch = new StopWatch();
watch.start();
@SuppressWarnings("unchecked")
Docket swaggerSpringMvcPlugin = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.genericModelSubstitutes(ResponseEntity.class).select().paths(Predicates.or(
//这里添加你需要展示的接口 根据自己的项目自己定义
PathSelectors.ant("/api/v1/**"),
PathSelectors.ant("/classes_list/**"),
PathSelectors.ant("/classes/**")
)
)
.build().securitySchemes(Collections.singletonList(apiKey()))
.groupName("G1")
.securityContexts(Collections.singletonList(securityContext()));
watch.stop();
logger.debug("Started Swagger in {} ms", watch.getTotalTimeMillis());
return swaggerSpringMvcPlugin;
}
private ApiInfo apiInfo() {
logger.info("api docs title : " + propertyResolver.getProperty("title"));
return new ApiInfo(propertyResolver.getProperty("title"), propertyResolver.getProperty("description"),
propertyResolver.getProperty("version"), propertyResolver.getProperty("termsOfServiceUrl"),
new Contact("", "", ""), propertyResolver.getProperty("license"),
propertyResolver.getProperty("licenseUrl"), new ArrayList<>());
}
private ApiKey apiKey() {
return new ApiKey("access_token", "Authorization", "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder().securityReferences(defaultAuth())
.forPaths(PathSelectors.regex(DEFAULT_INCLUDE_PATTERN)).build();
}
List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
return Collections
.singletonList(new SecurityReference("access_token", new AuthorizationScope[] { authorizationScope }));
}
}
三,controller类引用swagger
常用注解:
- @Api()用于类;
表示标识这个类是swagger的资源 - @ApiOperation()用于方法;
表示一个http请求的操作 - @ApiParam()用于方法,参数,字段说明;
表示对参数的添加元数据(说明或是否必填等) - @ApiModel()用于类
表示对类进行说明,用于参数用实体类接收 - @ApiModelProperty()用于方法,字段
表示对model属性的说明或者数据操作更改 - @ApiIgnore()用于类,方法,方法参数
表示这个方法或者类被忽略 - @ApiImplicitParam() 用于方法
表示单独的请求参数 - @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam
示例
@Api(tags = {"SpringBoot整合swagger-ui 测试方法"})
@RestController
@RequestMapping("/api/v1/testDemo")
public class DemoController {
@Autowired
QueryWeatherServiceImpl queryWeatherService;
@ApiOperation(value = "test01测试类")
@RequestMapping(value = "/test01",method = RequestMethod.GET)
public String test01(){
return "测试controller";
}
@ApiOperation(value = "查询天气工具",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RequestMapping(value = "/QueryWeather",method = RequestMethod.GET)
public void QueryWeather(){
queryWeatherService.QueryWeather();
}
}
四,运行效果
地址:http://localhost:8088/swagger-ui.html#/
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/cbc22896bf1eb36f82922c45d8c46a2b.png)
记录问题.提高自己.保护头发.从我做起。