文章优先发表在个人博客上面,后续更新可能忘记同步到CSDN,给你带来不便抱歉。
个人博客本篇文章地址:https://www.xdx97.com/article/688418128744415232
因为微服务是由多个服务构成如果每个服务的接口去开启新的页面这将十分难受。
下面教大家怎么把多个服务的API接口集成到一个页面中去。
文章目录
1、效果演示
2、代码部分
2-1:导入依赖包
<!-- swagger API文档插件 -->
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>1.7.0.RELEASE</version>
</dependency>
需要在zuul 、和你的服务里面都加入依赖。我这里有两个服务用户、工具所以我们需要添加三次。(你按照具体服务添加即可)
注:这个依赖可能会和SpringBoot版本冲突,1.7.0和boot2.0可以,但是boot2.0和1.9.0就报错,具体看你的版本。
2-2:配置yml文件
需要在你的服务里面配置下面的代码,你配置你的controller位置就好了
#swagger扫包
swagger:
base-package: com.xdx97.framework.controller
2-3:配置服务的Swagger2Config
这两个配置也是在你具体的服务里面配置的
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
public static final String VERSION = "1.0.0";
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
//设置文档的标题
.title("工具相关服务")
// 设置文档的描述
.description("工具相关服务 API 接口文档")
// 设置文档的版本信息-> 1.0.0 Version information
.version(VERSION)
// 设置文档的License信息->1.3 License information
.build();
}
}
2-3:配置zuul的SwaggerConfig
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;
import java.util.ArrayList;
import java.util.List;
@Component
@Primary
class SwaggerConfig implements SwaggerResourcesProvider {
@Override
public List<SwaggerResource> get() {
List resources = new ArrayList<>();
resources.add(swaggerResource("用户服务", "/xdx/myuser/v2/api-docs", "2.0"));
resources.add(swaggerResource("工具服务", "/xdx/mytools/v2/api-docs", "2.0"));
return resources;
}
private SwaggerResource swaggerResource(String name, String location, String version) {
SwaggerResource swaggerResource = new SwaggerResource();
swaggerResource.setName(name);
swaggerResource.setLocation(location);
swaggerResource.setSwaggerVersion(version);
return swaggerResource;
}
}
这里说一下上面代码中间的地址怎么配置。
我访问服务的接口是这样的:http://myzuul.com:9527/xdx/myuser/authority/menu/list
/xdx 是我的访问前缀,如果你没有你可以不要。
/myuser 是我的服务别名,你换成你的就好了。
假如说你的前缀是 /xxx, 服务名是 /yyy,那么你的地址就是: /xxx/yyy/v2/api-docs
3、具体应用
具体应用和SpringBoot是一样的,如果你会下面就可以不看了。
3-1:首先在controller配置注解@Api
@Api(tags = "用户相关接口", description = "提供用户相关的 Rest API")
3-2、get请求参数配置
@ApiOperation(value = "登录", notes = "创建人- 小道仙")
@ApiParam(value = "用户名", required = true)
3-3、post请求参数配置
3-3-1:先配置实体
@ApiModel("用户新增更新dto")
@ApiModelProperty("用户id")
3-3-2:配置接口
4、其它
4-1:全部注解可以看这里
https://www.leeyom.top/2017/09/25/tech-swagger-annotation/
https://www.ibm.com/developerworks/cn/java/j-using-swagger-in-a-spring-boot-project/index.html
4-2:常见错误
https://www.cnblogs.com/xiebq/p/9508848.html