引入依赖
<!--接口文档依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
配置类
@Component
@EnableOpenApi
@Data
public class SwaggerConfiguration {
@Bean
public Docket webApiDoc() {
return new Docket(DocumentationType.OAS_30)
.groupName("用户端接口文档")
.pathMapping("/")
// 定义是否开启swagger,false为关闭,可以通过变量控制,线上关闭
.enable(true)
//配置api文档元信息
.apiInfo(apiInfo())
// 选择哪些接口作为swagger的doc发布
.select()
.apis(RequestHandlerSelectors.basePackage("net.xdclass"))
//正则匹配请求路径,并分配至当前分组
.paths(PathSelectors.ant("/api/**"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("1024电商平台")
.description("微服务接口文档")
.contact(new Contact("小羊abc", "https://www.cnblogs.com/xiaoyangabc/", "[email protected]"))
.version("12")
.build();
}
}
简单使用
在 Controller 层类名上标注注解 @Api,方法上标注注解 @ApiParam
eg:
@Api(tags = "收货地址模块")
@RestController
@RequestMapping("/api/address/v1")
public class AddressController {
@Autowired
private AddressService addressService;
@ApiParam("根据id查找地址详情")
@GetMapping("/find/{address_id}")
public Object detail(@PathVariable("address_id")Long addressId){
return addressService.detail(addressId);
}
}
访问
ip地址:端口号/swagger-ui/index.html#/