1.添加以依赖
<!--添加swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.0</version>
</dependency>
2.创建swagger配置文件
@Configuration
@EnableSwagger2
//下面这两个注解是用于生成swagger json字符窜(访问http://localhost:8080/v2/api-docs 生成json),访问swagger页面就不行了。
@EnableWebMvc
@ComponentScan(basePackages = {“com.zcc.controller”})
public class Swagger2Configure {
@Value(“${swagger.enable}”)
private boolean enableSwagger;/在生产环境是不允许使用swagger,要设置为false
@Bean
public Docket createRestApi() {
return new Docket(SWAGGER_2)。enable(enableSwagger)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage(“com.zcc.controller”))//这里只能扫描包,如果这样写com.zcc.controller.UserController是不行的
.paths(PathSelectors.any())//可以根据url路径设置哪些请求加入文档,忽略哪些请求
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(“Spring Boot中使用Swagger2构建RESTful APIs”)//根据需要随便写
.description(“Spring Boot Swagger2”)//根据需要随便写
.contact(“seawater”)
.version(“1.0”)
.build();
}
}
3.访问地址
http://localhost:8080/swagger-ui.html //根据自己情况修改端口和ip
4.在controller层加注解 :这个类的作用是什么,比如:@Api(value=“用户模块”)
方法上的注解
//对方法的相关描述
@ApiOperation(value = “查询厂商详细信息”, notes = “根据url的厂商编号来查询”)
//对参数相关描述
@ApiImplicitParams({
@ApiImplicitParam(name = “zccParam”, value = “厂商查询参数”, required = true, dataType = “ZccParam”)
})
5.使用junit测试工具进行本地测试代码,要加注解@WebAppConfiguration,如果不用swagger就不用加
6.将生成的swagger json窜放入SwaggerToWord项目
更多烟台培训相关资讯,请扫描下方二维码