文章優先發表在個人博客上面,後續更新可能忘記同步到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