引入两依赖
<!-- swagger核心--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swagger的ui界面,前缀/swagger-ui.html--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency><!-- bootstrap的swagger-ui界面,前缀/doc.html-->
<dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency>
启动类加上@EnableSwagger2注解
springboot2.6以上url查找规则改了
spring: mvc: pathmatch: matching-strategy: ant_path_matcher
添加配置类自定义ui界面信息
@Configuration
public class SwaggerConfig{
@Bean
public Docket docket(){
Docket docket = new Docket(DocumentationType.SWAGGER_2);
ApiInfo apiInfo = new ApiInfoBuilder()
.title("动力节点金融项目")
.version("1.0")
.description("前后端分离项目,前端Vue,后端SpringBoot+Dubbo分布式项目")
.contact(new Contact("动力bj2114班级", "https://www.baidu.com/", "wangli7324@qq.com"))
.license("Apache 2.0")
.build();
docket = docket.apiInfo(apiInfo);
return docket;
}
}
@RestController
@Api(tags = "hello控制器")//默认controller名称
public class MyController {
@ApiOperation(value = "输出Swagger的功能", notes = "控制器第一个方法")//默认输出方法名
@ApiImplicitParams({
@ApiImplicitParam(name = "name", value = "用户姓名", required = true, defaultValue = "zhangsan",
dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "id", value = "用户主键", required = true, defaultValue = "12",
dataType ="int",paramType="query")
})
@GetMapping("/hello")
public String helloSwagger(String name, Integer id) {
return "Swagger在线生成接口文档,同时具有测试接口的功能";
}
}