网关和需要swagger的pom中添加
<!-- swagger cloud start -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- swagger cloud end -->
网关中zuul的配置
zuul:
# SendErrorFilter:
# post:
# disabled: true #自定义异常
#设置忽略的服务,即配置后将不会被路由(但对于明确配置在路由中的,将不会被忽略)
ignored-services: "*"
sensitive-headers:
routes:
xxxxxx:#微服务名称
path: #路径
serviceId: #微服务id
swagger配置文件
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
/* @Resource
private YYcloudProperties yycloudProperties;
*/
/* @Value("${yycloud.config.gateway-url}")
private String GATEWAY_URL;
*/
/**
* 主要是这个方法,其他的方法是抽出去的,所以大家不要害怕为啥有这么多方法
* 在 basePackage 里面写需要生成文档的 controller 路径
*/
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
/* .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))*/
.apis(RequestHandlerSelectors.basePackage("com.yonyou.supplier.controller"))
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo())
.securitySchemes(Collections.singletonList(securityScheme()))
.securityContexts(Collections.singletonList(securityContext()));
}
/**
* 这个方法主要是写一些文档的描述
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("相关描述")
.termsOfServiceUrl("http:......").contact("sakura").version("1.0").build();
/*SwaggerProperties swagger = yycloudProperties.getSwagger();
return new ApiInfo(
swagger.getTitle(),
swagger.getDescription(),
swagger.getVersion(),
"",
new Contact(swagger.getContactName(), swagger.getContactUrl(), swagger.getContactEmail()),
"", "", Collections.emptyList());*/
}
/**
* 这个类决定了你使用哪种认证方式,我这里使用密码模式
* 其他方式自己摸索一下,完全莫问题啊
*/
private SecurityScheme securityScheme() {
// GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("http://" + GATEWAY_URL + "/oauth-api/oauth/token");
GrantType grantType = new ResourceOwnerPasswordCredentialsGrant("http://127.0.0.1:30003/oauth-api/auth/form");
return new OAuthBuilder()
.name("spring_oauth")
.grantTypes(Collections.singletonList(grantType))
.scopes(Arrays.asList(scopes()))
.build();
}
/**
* 这里设置 swagger2 认证的安全上下文
*/
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(Collections.singletonList(new SecurityReference("spring_oauth", scopes())))
.forPaths(PathSelectors.any())
.build();
}
/**
* 这里是写允许认证的scope
*/
private AuthorizationScope[] scopes() {
return new AuthorizationScope[]{
new AuthorizationScope("*", "All scope is trusted!")
// new AuthorizationScope("all", "All scope is trusted!")
};
}
// @Bean
// public SecurityConfiguration securityInfo() {
// return new SecurityConfiguration(null, null, null, null, "", ApiKeyVehicle.HEADER,"Authorization",": Bearer");
// }
}
将各个服务集成到swagger中的配置文件
@Primary
@Component
public class DocumentationConfig implements SwaggerResourcesProvider {
private final RouteLocator routeLocator;
public DocumentationConfig(RouteLocator routeLocator) {
this.routeLocator = routeLocator;
}
public List<SwaggerResource> get() {
List<SwaggerResource> resources = new ArrayList<>();
List<Route> routes = routeLocator.getRoutes();
routes.forEach(route -> resources.add(swaggerResource(route.getId(), route.getFullPath().replace("/**", "/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;
}
}
DocumentationConfig和SwaggerConfiguration放在网关启动类同目录下,要用swagger的微服务启动类中加注解@EnableSwagger2
访问地址
http://ip:网关端口/swagger-ui.html
选择需要查看的微服务
controller上的注解
@Api(produces = MediaType.APPLICATION_JSON_UTF8_VALUE,description = "xxxcontroller说明")
具体方法上的参数注解
@PostMapping("/add") @ApiOperation(httpMethod = "POST",value = "保存供应商注册信息") public Wrapper<Integer> add(HttpServletRequest request, @ApiParam(name = "supplierVO", value = "查询公告条件") @RequestBody SupplierVO supplierVO){ getTycInfo.getSupplierByIdAndName("3355627294","全维度测试有限责任公司"); return WrapMapper.ok(suppilerService.insert(supplierVO)); }