swagger是什么以及使用的好处,网上随便搜索就有,就不赘述了,下面主要说明swagger和springmvc配置的步骤。
首先声明一点,由于配置过程中查看了较多的文章,无法一一标明引用地址,在此感谢各位的分享
经过一天多的搜索,swagger现在在网上有大致两种,一种是springfox-swagger
另一种就是springfox-swagger2
两种教程都很多,配置也都大概一致,但是会有一些细微的差别,在此做个全面的总结。
环境及工具:JDK 1.8 Intellij IDEA 2017.2 maven 3.3.9 tomcat 8
maven依赖:
<!-- springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.0</version>
</dependency>
<!-- springfox-swagger2 dependencies -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.5</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>15.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml</groupId>
<artifactId>classmate</artifactId>
<version>1.1.0</version>
</dependency>
<!--swagger end-->
swagger配置
package com.qinzx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author: qinzx
* @date: 2018/5/30 14:03
* @modify by:
*/
@Component
@Configuration
@EnableSwagger2
@EnableWebMvc
@ComponentScan("com.qinzx.controller")
public class Swagger2Config {
@Bean
public Docket createAPI() {
return new Docket(DocumentationType.SWAGGER_2).forCodeGeneration(true).select().apis(RequestHandlerSelectors.any())
//过滤生成链接
.paths(PathSelectors.any()).build().apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
Contact contact=new Contact("赵帅","http://blog.maileba.top","zhaoshuaivov@163.com");
ApiInfo apiInfo = new ApiInfoBuilder().license("Apache License Version 2.0").title("Swagger 集成测试").description("Swagger API Teste").contact(contact).version("1.0").build();
return apiInfo;
}
}
sping-context.xml配置
<!-- scan the package and the sub package -->
<context:component-scan base-package="com.qinzx.controller"/>
<context:component-scan base-package="springfox"/>
<!-- don't handle the static resource -->
<mvc:default-servlet-handler />
<!-- if you use annotation you must configure following setting -->
<mvc:annotation-driven />
<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
controller注解
@ApiOperation(value = "根据用户名获取用户对象", httpMethod = "GET", response = ResponseMsg.class, notes = "根据用户名获取用户对象")
@RequestMapping("/list")
@ResponseBody
public ResponseMsg getList() {
return new ResponseMsg().success(new ArrayList<String>());
}
配置好这些,访问地址各式为{ip}:{port}/{project-name}/swagger-ui.html,没有project-name就省略,例如:
http://localhost:8088/swagger-ui.html
暂时就这些,有疑问欢迎留言。
天下武功,唯快不破!