直接进入正题:
第一步:jar包的引入:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.4.0</version>
</dependency>
网上有人说jar包的引入可能会出现版本的问题,可能需要与你的编辑器或者jdk要匹配,如果遇到这个问题,可能需要尝试引入不同的版本试试,我个人没有遇到这个情况。
第二步:swagger的配置启动类编写:
通过@Configuration注解,表明它是一个配置类,@EnableSwagger2开启swagger2。apiINfo()配置一些基本的信息。apis()指定扫描的包会生成文档。
我放在了和启动类同级目录。
具体配置类如下:
package xxx;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
@Configuration
@EnableSwagger2
public class Swagger2 {
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
//为当前包路径
.apis(RequestHandlerSelectors.basePackage("com.baidu.supply.runner"))
.paths(PathSelectors.any())
.build();
}
//构建 api文档的详细信息函数,注意这里的注解引用的是哪个
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//页面标题
.title("Spring Boot 测试使用 Swagger2 构建RESTful API")
//创建人
.contact(new Contact("baidu", "http://www.baidu.com", ""))
//版本号
.version("1.0")
//描述
.description("API 描述")
.build();
}
}
第三步:使用swagger来进行模拟测试:
SwaggerDemoController类:
package xxx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
/**
* Created by Administrator on 2017/9/13.
*/
@RestController
@RequestMapping("api")
@Api("swaggerDemoController相关的api")
public class SwaggerDemoController {
@Autowired
private StudentService studentService;
private static final Logger logger = LoggerFactory.getLogger(SwaggerDemoController.class);
@ApiOperation(value = "根据id查询学生信息", notes = "查询数据库中某个的学生信息")
@ApiImplicitParam(name = "id", value = "学生ID", paramType = "path", required = true, dataType = "Integer")
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Student getStudent(@PathVariable int id) {
logger.info("开始查询某个学生信息");
return studentService.selectStudentById(id);
}
}
Student类:
package xxx;
public class Student {
private int id;
private String name;
public Student(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
StudentService类:
package xxx;
import org.springframework.stereotype.Service;
@Service
public class StudentService {
public Student selectStudentById(int id) {
return new Student(id);
}
}
启动项目:
访问路径:
http://localhost:端口/swagger-ui.html
访问报错:
这个错误,是因为资源映射问题导致。
我们在访问 http://localhost:端口/swagger-ui.html 时,这个swagger-ui.html相关的所有前端静态文件都在springfox-swagger-ui-2.4.0.jar里面。目录如下:
Spring Boot自动配置本身不会自动把/swagger-ui.html这个路径映射到对应的目录META-INF/resources/下面。我们加上这个映射即可。
@Configuration
@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
再次访问:
点击下图红框内:
展开之后,输入id,可以点击下图中的 Try it out!
最后得到响应:
至此已经完成了一个简单的接口测试。
好了,swagger2与springboot的集成完毕,是不是很方便。
参考资料:
https://www.cnblogs.com/fengli9998/p/7522973.html
https://blog.csdn.net/keplerpig/article/details/78601157
https://blog.csdn.net/u013128651/article/details/78864876