springboot整合swagger
创建springboot项目之后,加入swagger依赖
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency>
配置swagger
@Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //扫描的包 .apis(RequestHandlerSelectors.basePackage("cn.wilian.demo.Controller")) .paths(PathSelectors.any()) .build(); } //构建 api文档的详细信息函数,注意这里的注解引用的是哪个 private ApiInfo apiInfo() { return new ApiInfoBuilder() //页面标题 .title("Spring Boot 整合swagger") //创建人 .contact(new Contact("wilian", "www.baidu.com", "")) //版本号 .version("1.0") //描述 .description("第一个swagger小demo") .build(); }
编写控制器
@RestController @RequestMapping("api") @Api("swaggerDemoController相关的api") public class SwaggerDemoController { @RequestMapping(value = "/{id}", method = RequestMethod.GET) public String getStudent(@PathVariable int id) { System.out.println(id); return "swagger"; }
项目的大概目录
启动项目
访问http://localhost:8080/swagger-ui.html
过程遇到的问题
1.引入EnableSwagger2的时候,报错,提示创建这个类,解决方法:确定依赖没有错误,已经下载好jar之后,重启IDEA
2.在配置swagger的时候,没有引入 springfox.documentation.service.Contact;而是自己写了一个Contact类,导致了错误