今天简单对Swagger2做了个集成测试,记录一下
集成Swagger2首先是引入Swagger依赖
pom.xml增加如下配置:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
其次是增加Swagger2配置类:
package com.base.myweb;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.base.myweb.controller")) //此处配置接口路径
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("springboot利用swagger构建api文档") //标题
.description("简单优雅的restfun风格,http://blog.csdn.net/saytime") //描述
.termsOfServiceUrl("http://blog.csdn.net/saytime") //自定义链接,此处引用别人的博文,地址为博主主页
.version("1.0")
.build();
}
}
下面在启动类中增加@EnableSwagger2注解
package com.base.myweb;
//import com.base.myweb.annotation.EnableHelloWorld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/*import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;*/
@SpringBootApplication
/*
@EnableEurekaClient
@EnableFeignClients
*/
@EnableSwagger2
public class MywebApplication {
public static void main(String[] args) {
SpringApplication.run(MywebApplication.class, args);
}
}
最后就是我们的接口进行配置
package com.base.myweb.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.base.myweb.mapper.UserInfoMapper;
import com.base.myweb.pojo.Userinfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
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 java.util.List;
@RestController
@Api(value = "集成测试", tags = "Swagger集成测试")
public class SwaggerController {
@Autowired
UserInfoMapper userInfoMapper;
@ApiOperation(value="我是Swagger的接口简述", notes="Swagger接口详细描述信息")
@ApiImplicitParam(name = "id", value = "用户ID", dataType = "String", paramType = "path" )
@RequestMapping(value = "/swagger/{id}", method = RequestMethod.GET)
public List<Userinfo> getUserById (@PathVariable(value = "id") String id){
List<Userinfo> list = userInfoMapper.selectList(new QueryWrapper<Userinfo>());
return list;
}
}
到这里我们的集成就完成了,Swagger2提供了可视化的界面,在项目启动后,访问http://localhost:8080/swagger-ui.html#/即可看到我们的接口信息,内容如下:
接口详细信息如下,直接在该界面进行调试
最后是一些Swagger注解说明:
swagger通过注解表明该接口会生成文档,包括接口名、请求方法、参数、返回信息的等等。
@Api:修饰整个类,描述Controller的作用
@ApiOperation:描述一个类的一个方法,或者说一个接口
@ApiParam:单个参数描述
@ApiModel:用对象来接收参数
@ApiProperty:用对象接收参数时,描述对象的一个字段
@ApiResponse:HTTP响应其中1个描述
@ApiResponses:HTTP响应整体描述
@ApiIgnore:使用该注解忽略这个API
@ApiError :发生错误返回的信息
@ApiImplicitParam:一个请求参数
@ApiImplicitParams:多个请求参数