一: 什么是 swagger?
?swagger 是一款非常好用的写 API 文档的框架. 其他自行百度
二: ssm 整合 swagger?
1: 在 maven 的 pom 文件中引入依赖:(注意版本, 否则会导致 tomcat 不能正常启动)
- io.springfox
- springfox-swagger2
- 2.7.0
- io.springfox
- springfox-swagger-ui
- 2.7.0
- com.fasterxml.jackson.core
- jackson-databind
- 2.9.0
2: 创建 SwaggerConfig.java 文件, 即: swagger 的配置文件, 最好放到单独的文件夹下
- @webAppConfiguration
- @EnableSwagger2
- @EnableWebMvc
- @ComponentScan(basePackages="com.zgz.cn.controller")
- public class SwaggerConfig {
- @Bean
- public Docket API() {
- return new Docket(DocumentationType.SWAGGER_2)
- .select()
- .apis(RequestHandlerSelectors.any())
- .build()
- .apiInfo(apiInfo());
- }
- private ApiInfo apiInfo() {
- return new ApiInfoBuilder()
- .title("xxx 项目接口文档")
- .description("xxx 项目接口测试")
- .version("1.0.0")
- .termsOfServiceUrl("")
- .license("")
- .licenseUrl("")
- .build();
- }
- }
3: 在 SpringMVC 的配置文件中配置:(配置所有控制器所在的包)
- class="com.zgz.cn.swagger.SwaggerConfig"/>
- location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
- location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
4: 在 Web.xml 中配置所有的请求都经过 DispatcherServlet 处理
- springMVC
- /
5: 在控制器中使用注解:(swagger 常用注解 https://segmentfault.com/a/1190000010465989 )
6: 测试 (访问地址: 项目名 / swagger-ui.HTML )
来源: http://www.bubuko.com/infodetail-2819596.html