我们知道,Swagger2整合到项目中,可以非常方便地进行接口测试,是前后端对接效率提高。现在,我们可以在Zuul中整合Swagger2,通过Zuul配置文件配置的映射路径,来生成源服务接口的测试Dashboard。
1、zuul-service中配置
1、1 pom.xml中引入swagger2相关jar包
...
<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>
...
1、2 配置swagger2
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* 请求地址:http://localhost:18600/swagger-ui.html
* @author jackcooper
* @create 2018-06-25 17:55
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Autowired
private DiscoveryClientRouteLocator discoveryClientRouteLocator;
@Primary
@Bean
public SwaggerResourcesProvider swaggerResourcesProvider() {
return new SwaggerResourcesProvider() {
@Override
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
for (Route route : discoveryClientRouteLocator.getRoutes()) {
resources.add(createResource(route.getLocation(),route.getId(), "2.0"));
}
return resources;
}
};
}
private SwaggerResource createResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation("/" + location + "/v2/api-docs");
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
2、源服务中配置
2.1 pom.xml中引入依赖
...
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
...
2.2 Swagger2配置类
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* @author jackcooper
* @create 2017-07-03 12:34
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket swaggerPersonApi10() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("cn.springcloud.sample.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(
new ApiInfoBuilder()
.version("1.0")
.title("Original Service API")
.description("Original Service API v1.0")
.build()
);
}
}
2.3 Controller添加Swagger2的注解
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@Api(description = "测试源服务API接口")
@RestController
public class TestController {
@ApiOperation(value = "加法", notes = "加法")
@GetMapping("/add")
public Integer add(Integer a, Integer b){
return a + b;
}
@ApiOperation(value = "减法", notes = "减法")
@GetMapping("/sub")
public Integer sub(Integer a, Integer b){
return a - b;
}
@ApiOperation(value = "乘法", notes = "乘法")
@GetMapping("/mul")
public Integer mul(Integer a, Integer b){
return a * b;
}
@ApiOperation(value = "除法", notes = "除法")
@GetMapping("/div")
public Integer div(Integer a, Integer b){
return a / b;
}
}
swagger2注解说明:
详解:https://my.oschina.net/zzuqiang/blog/793606
@Api:用在请求的类上,表示对类的说明
tags="说明该类的作用,可以在UI界面上看到的注解"
value="该参数没什么意义,在UI界面上也看到,所以不需要配置"
@ApiOperation:用在请求的方法上,说明方法的用途、作用
value="说明方法的用途、作用"
notes="方法的备注说明"
@ApiImplicitParams:用在请求的方法上,表示一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
dataType:参数类型,默认String,其它值dataType="Integer"
defaultValue:参数的默认值
@ApiResponses:用在请求的方法上,表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:用于响应类上,表示一个返回响应数据的信息
(这种一般用在post创建的时候,使用@RequestBody这样的场景,
请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:用在属性上,描述响应类的属性
3、 最终效果图
运行所有微服务,请求网关地址(zuul-server)http://ip:18600/swagger-ui.html地址。
image.png参考文章:swagger2注解说明:https://blog.csdn.net/xiaojin21cen/article/details/78654652
网友评论