spring cloud微服务使用swagger2接口文档

​​​​第一步. 自动配置类

主启动类添加:@SpringCloudApplication


gateway服务 POM添加:


        
            org.springframework.cloud
            spring-cloud-starter-gateway
        
        
        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        
        
        
            org.springframework.boot
            spring-boot-starter-validation
        


前言

提示:主要内容都在gateway服务


提示:以下是本篇文章正文内容,下面案例可供参考


一、Swagger提供给前端配置获取注册中心启动的服务


/**
 * @Author wangZhiDong
 * @Date 2020/12/16
 * Swagger提供给前端 -基础配置
 **/
@Component
public class MySwaggerResourceProvider implements SwaggerResourcesProvider {

    /**
     * swagger2默认的url后缀
     */
    private static final String SWAGGER2URL = "/v2/api-docs";

    /**
     * 网关路由
     */
    private final RouteLocator routeLocator;

    /**
     * 网关应用名称
     */
    @Value("${spring.application.name}")
    private String self;

    @Autowired
    public MySwaggerResourceProvider(RouteLocator routeLocator) {
        this.routeLocator = routeLocator;
    }

    @Override
    public List get() {
        List resources = new ArrayList<>();
        List routeHosts = new ArrayList<>();
        // 由于我的网关采用的是负载均衡的方式,因此我需要拿到所有应用的serviceId
        // 获取所有可用的host:serviceId
        routeLocator.getRoutes().filter(route -> route.getUri().getHost() != null)
                //.filter(route -> !self.equals(route.getUri().getHost()))
                .subscribe(route -> routeHosts.add(route.getUri().getHost()));

        // 记录已经添加过的server,存在同一个应用注册了多个服务在nacos上
        Set dealed = new HashSet<>();
        routeHosts.forEach(instance -> {
            // 拼接url,样式为/serviceId/v2/api-info,当网关调用这个接口时,会自动通过负载均衡寻找对应的主机
            String url = "/" + instance + SWAGGER2URL;
            if (!dealed.contains(url)) {
                dealed.add(url);
                SwaggerResource swaggerResource = new SwaggerResource();
                swaggerResource.setUrl(url);
                swaggerResource.setName(instance);
                resources.add(swaggerResource);
            }
        });
        return resources;
    }
}


二、Swagger提供给前端页面的接口


代码如下(示例):

/**
 * @Author wangZhiDong
 * @Date 2020/12/16
 * Swagger提供给前端页面的接口
 **/
@RestController
@RequestMapping("/swagger-resources")
public class SwaggerResourceController {
    private MySwaggerResourceProvider swaggerResourceProvider;

    @Autowired
    public SwaggerResourceController(MySwaggerResourceProvider swaggerResourceProvider) {
        this.swaggerResourceProvider = swaggerResourceProvider;
    }

    @RequestMapping(value = "/configuration/security")
    public ResponseEntity securityConfiguration() {
        return new ResponseEntity<>(SecurityConfigurationBuilder.builder().build(), HttpStatus.OK);
    }

    @RequestMapping(value = "/configuration/ui")
    public ResponseEntity uiConfiguration() {
        return new ResponseEntity<>(UiConfigurationBuilder.builder().build(), HttpStatus.OK);
    }

    @RequestMapping
    public ResponseEntity> swaggerResources() {
        return new ResponseEntity<>(swaggerResourceProvider.get(), HttpStatus.OK);
    }
}


2.接口放行

代码如下(示例):

            "/**/v2/api-docs/**",
            "/v2/api-docs-ext/**",
            "/swagger/api-docs",
            "/swagger-ui.html",
            "/doc.html",
            "/swagger-resources/**",


三、SpringCloud其它需要Swagger的其它子服务

POM添加:

        
        
            io.springfox
            springfox-swagger2
            2.9.2
        

你可能感兴趣的:(spring,cloud,微服务,spring)