引入依赖
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
创建swagger2配置类
package com.gr.ggkt.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.service.Contact;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2 //开启Swagger2
public class Swagger2Config {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("ggkt")
.apiInfo(webApiInfo())
.select()
//只显示api路径下的页面
//.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("gr", "http://gr.com", "gr.com"))
.build();
}
}
其他子模块使用要引入依赖 如:
<dependency>
<groupId>com.gr</groupId>
<artifactId>service_utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
使用的模块主启动类上添加注释 @ComponentScan("com.gr")
测试:启动项目,输入网址: localhost:8301/swagger-ui.html (8301替换为要测试项目端口号)
swagger页面中文显示端口信息:
定义在类上:@Api
定义在方法上:@ApiOperation
定义在参数上:@ApiParam
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping("/admin/vod/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
//查询所有的讲师
@GetMapping("findAll")
@ApiOperation("所有讲师列表")
public List<Teacher> findAll(){
List<Teacher> list = teacherService.list();
return list;
}
//逻辑删除讲师
@ApiOperation("逻辑删除讲师")
@DeleteMapping("remove/{id}")
public boolean removeTeacher(@PathVariable("id")Long id){
boolean b = teacherService.removeById(id);
return b;
}
}