赞
踩
Swageer 协调前后端,可测试后端接口,最流行的API框架,RestFul 风格Api文档在线自动生成工具
swagger 简单使用
1、导入依赖
- <!-- swagger依赖-->
- <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger2</artifactId>
- <version>3.0.0</version>
- </dependency>
- <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
- <dependency>
- <groupId>io.springfox</groupId>
- <artifactId>springfox-swagger-ui</artifactId>
- <version>3.0.0</version>
- </dependency>
2.创建Hello工程
3、配置swagger2
- @Configuration
- @EnableSwagger2 //开启swagger
- public class SwaggerConfiger {
-
- }
运行测试,可通过http://localhost:8080/swagger-ui.html访问
因为资源路径映射问题,SpringBoot无法找到swagger-ui.html可以添加配置解决
- @Configuration
- public class WebMvcConfig extends WebMvcConfigurerAdapter {
- /**
- * 访问静态资源
- * */
- @Override
- public void addResourceHandlers(ResourceHandlerRegistry registry) {
- /**
- * SpringBoot自动配置本身并不会把/swagger-ui.html
- * 这个路径映射到对应的目录META-INF/resources/下面
- * 采用WebMvcConfigurerAdapter将swagger的静态文件进行发布;
- */
- registry.addResourceHandler("swagger-ui.html")
- .addResourceLocations("classpath:/META-INF/resources/");
-
- registry.addResourceHandler("/webjars/**")
- .addResourceLocations("classpath:/META-INF/resources/webjars/");
- //将所有/static/** 访问都映射到classpath:/static/ 目录下
- registry.addResourceHandler("/static/**").addResourceLocations(ResourceUtils.CLASSPATH_URL_PREFIX +"/static/");
- super.addResourceHandlers(registry);
- }
-
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
如果还是不行,检查是否依赖版本过高,测试3.0.0在添加上面的映射路径后还是无效,换低版本后成功。
参考
https://blog.csdn.net/lovequanquqn/article/details/90705721
https://www.cnblogs.com/yuanweidao/p/13710194.html
自定义配置Swagger
- @Configuration
- @EnableSwagger2 //开启swagger
- public class SwaggerConfiger {
-
- @Bean
- public Docket docket(){
- return new Docket(DocumentationType.SWAGGER_2)
- .apiInfo(apiInfo())
- // 分组名字
- .groupName("看向未来213")
- //设置扫描路径,默认全扫描
- .select()
- // any 扫描全部
- //none 不扫描
- //basePackage 扫描包
- //withMethodAnnotation 按特定方法上注解类扫描
- //withClassAnnotation 按特定类注解扫描
- .apis(RequestHandlerSelectors.basePackage("com.example.springbootswageer.controller"))
- // .paths(PathSelectors.ant("com/**")) //过滤
- .build();
- }
-
- public ApiInfo apiInfo(){
- Contact contact = new Contact("看向未来213", "https://blog.csdn.net/qq_56800327?spm=1000.2115.3001.5343", "2621302586qq.com");
- return new ApiInfo(
- "Api Documentation",
- "Swagger初识",
- "v1.0",
- "https://blog.csdn.net/qq_56800327?spm=1000.2115.3001.5343",
- contact,
- "Apache 2.0",
- "http://www.apache.org/licenses/LICENSE-2.0",
- new ArrayList());
- }
- }
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
配置APi文档的分组
.groupName("看向未来213")
给Api文档添加注释的五个注解
- @Api("用户实体类")
- @ApiModel("用户实体类")
- 两个一样是标注实体类的注解
-
-
- @ApiModelProperty("用户名") 给属性添加注解
-
- @ApiOperation("方法注释") 给方法添加注解
-
- @ApiParam("用户名") 给参数加注解
try it out 可以在网站上测试,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。