1、引入依赖
<!-- 此处使用1.9会报错,springboot2.1.7,原因未明 -->
<dependency> <groupId>com.spring4all</groupId> <artifactId>swagger-spring-boot-starter</artifactId> <version>1.7.0.RELEASE</version> </dependency>
2、启用注解
在启动类添加启用注解@EnableSwagger2
@Configuration @EnableSwagger2 public class SwaggerConfig { private ApiInfo apiInfo() { return new ApiInfoBuilder().title("API接口文档") .description("用户信息管理") .version("1.0.0") .build(); } @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) //这里写的是API接口所在的包位置 .paths(PathSelectors.any()) .build(); } }
3、分组
swagger.docket.controller.title=group-controller swagger.docket.controller.base-package=com.mashibing.springboot.controller swagger.docket.restcontroller.title=group-restcontroller swagger.docket.restcontroller.base-package=com.mashibing.springboot.controller.rest
4、接口类描述
@Api(value = "用户权限管理",tags={"用户操作接口"})
5、
@ApiOperation(value = "添加权限") @PostMapping("update") public RespStat update(@ApiParam(name="permission",required = true, example = "{json}",value = "权限对象") @RequestBody Permission permission) { System.out.println("permission:" + ToStringBuilder.reflectionToString(permission)); permissionSrv.update(permission); return RespStat.build(200); }
6、实体模型
//用于实体类中
@ApiModelProperty(value = "权限id", name = "id",dataType = "integer",required = true,example = "1") private Integer id;
7、访问
http://localhost:8080/swagger-ui.html