在springboot中使用Swagger自动化生成restapi接口文档
2017-06-27 00:00
387 查看
首先,在pom.xml文件中,加入引用包:
然后在工程中,创建Swagger配置文件。注意,在配置文件中,指定Swagger扫描的包:
最后,在启动程序中,将根目录指向Swagger自动生成的API展示页面/swagger-ui.html:
重启应用,就能够看到API接口文档:
最后,需要注意的是,在controller文件中,所有方法必须指定方法名,否则Swagger会自动生成get、post、delete、options、header、put、trace 共7种方法的接口文档,即便没有使用:
<!-- https://mvnrepository.com/artifact/io.swagger/swagger-annotations --> <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配置文件。注意,在配置文件中,指定Swagger扫描的包:
package com.my.conf; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2).select() .apis(RequestHandlerSelectors.basePackage("com.my.controller")) // 对controller下所有api进行监控 .paths(PathSelectors.any()) // 对所有路径进行监控 .build(); } }
最后,在启动程序中,将根目录指向Swagger自动生成的API展示页面/swagger-ui.html:
package com.my; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Controller @SpringBootApplication public class Application extends WebMvcConfigurerAdapter { public static void main(String[] args) { SpringApplication.run(Application.class, args); } @RequestMapping("/") String home() { return "redirect:/swagger-ui.html"; } }
重启应用,就能够看到API接口文档:
最后,需要注意的是,在controller文件中,所有方法必须指定方法名,否则Swagger会自动生成get、post、delete、options、header、put、trace 共7种方法的接口文档,即便没有使用:
@RequestMapping(value="/getUserInfo",method=RequestMethod.GET)
相关文章推荐
- 使用Spring Boot&Swagger快速构建REST API并生成优美的API文档
- spring boot项目使用swagger-codegen生成服务间调用的jar包
- spring boot 中使用swagger 来自动生成接口文档
- 使用 SpringBoot + Swagger 生成接口 API 文档
- 使用Swagger生成Spring Boot REST客户端(支持Feign)(待实践)
- 使用Swagger生成JAVA Mock Server(Springboot)代码
- Spring boot结合swagger自动生成api文档
- 使用Swagger在SpringBoot项目中管理API文档(使用Oauth2)
- Swagger集成Springboot生成Restful api
- Swagger2 生成 Spring Boot API 文档
- spring boot利用swagger和spring doc生成在线和离线文档
- 使用Swagger实现webapi接口自动化文档生成
- SwaggerUI自动生成API文档(SwaggerUI+SpringBoot)
- springboot的restController使用swagger遇到的问题。
- 使用SwaggerSpringBootStarter展示web项目api接口
- spring boot rest接口自动生成文档(包含swagger)
- Spring Boot如何让Web API自动生成文档,并解决swagger-annotations的API注解description属性废弃的问题
- Spring boot 中使用swagger-ui实现 restful-api
- springboot+springfox+Swagger 实现项目的restful文档的自动生成
- SpringBoot&Swagger构建REST API并生成API文档