引入依赖
<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>
</dependencies>
swagger配置
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket getUserDocket(){
ApiInfo apiInfo=new ApiInfoBuilder()
.title("用户管理")//api标题
.description("用户管理相关接口描述")//api描述
.version("1.0.0")//版本号
.build();
return new Docket(DocumentationType.SWAGGER_2)//文档类型(swagger2)
.apiInfo(apiInfo)//设置包含在json ResourceListing响应中的api元信息
.select()//启动用于api选择的构建器
.apis(RequestHandlerSelectors.basePackage("com.testing.myswagger2.controller"))//扫描接口的包
.paths(PathSelectors.any())//路径过滤器(扫描所有路径)
.build();
}
}
controller控制
@RestController
@Api
public class HelloController {
@ApiOperation(value = "你好",notes = "这里是你好界面")
@GetMapping("hello")
public Book hello(){
Book book=new Book("西游记","叶伟民");
return book;
}
@ApiOperation(value = "你不好",notes = "这里是你不好界面")
@GetMapping("nohello")
public Book NOhello(){
Book book=new Book("三国演义","大司马");
return book;
}
@ApiOperation(value = "人的信息",notes = "这里是人界面")
@GetMapping("person")
public Person person(){
Person person=new Person("大司马","18",true);
return person;
}
}
controller上要加上@Api
@ApiOperation
value
是在主界面显示的字,notes
相当于备注
结果