导入Maven依赖
<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>
接口文档生成类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket UserApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(UserApiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.calf.controller.user"))
.paths(PathSelectors.any())
.build()
.groupName("用户中心");
}
private ApiInfo UserApiInfo() {
return new ApiInfoBuilder()
.title("测试项目标题")
.description("接口描述")
.contact(
new Contact("名字", "网址", "邮箱")
)
.version("1.0.0-SNAPSHOT")
.build();
}
}
单一接口配置(对应Controller)
@RestController
@Api(value = "@Api 修饰整个类,描述Controller的作用")
public class SwaggerController {
@GetMapping("/hello/{name}")
@ApiOperation(value = "描述一个类的方法,或者说是一个接口")
public String hello(
@ApiParam(value = "单个参数的描述",example = "举一个例子")@PathVariable(value = "name") String name){
return "hello" + name;
}
}