spring cloud zuul集成swagger2

spring cloud zuul集成swagger2

  1. 依赖版本
    spring cloud <Greenwich.SR1>
    spring boot <2.1.3.RELEASE>

  2. 添加依赖(zuul/service)

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
    </dependency>
    <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>
    
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>swagger-bootstrap-ui</artifactId>
        <version>1.9.0</version>
    </dependency>
    
  3. zuul/service 启动类添加@EnableSwagger2注解

  4. zuul 添加configuration

    @Configuration
    @EnableSwaggerBootstrapUI
    @Primary
    public class SwaggerConfig implements SwaggerResourcesProvider {
        //是否开启swagger,正式环境一般是需要关闭的
        @Value("${swagger2.enable}")
        Boolean swaggerEnabled;
    
        @Autowired
        RouteLocator routeLocator;
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                    // 是否开启
                    .enable(swaggerEnabled).select()
                    // 扫描的路径包
                    .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                    // 指定路径处理PathSelectors.any()代表所有的路径
                    .paths(PathSelectors.any()).build().pathMapping("/");
        }
    
        //设置api信息
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("路由网关(Zuul):利用swagger2聚合API文档")
                    .description("网关整合swagger2")
                    // 作者信息
                    .contact(new Contact("****", "****", "****"))
                    .version("1.0.0")
                    .termsOfServiceUrl("******")
                    .build();
        }
    
        @Override
        public List<SwaggerResource> get() {
            //利用routeLocator动态引入微服务
            List<SwaggerResource> resources = new ArrayList<>();
            resources.add(swaggerResource("zuul","/v2/api-docs","1.0"));
            //循环 使用Lambda表达式简化代码
            routeLocator.getRoutes().forEach(route ->{
                //动态获取
                resources.add(swaggerResource(route.getId(),route.getFullPath().replace("**", "v2/api-docs"), "1.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;
        }
    }
    
  5. service 上添加configuration

    @Configuration
    @EnableSwaggerBootstrapUI
    public class SwaggerConfig {
        //是否开启swagger,正式环境一般是需要关闭的
        @Value("${swagger2.enable}")
        Boolean swaggerEnabled;
    
        @Bean
        public Docket orderApis() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .enable(swaggerEnabled)
                    .select()
                    .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                    .paths(PathSelectors.any()).build().pathMapping("/");
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("****")
                    .description("****")
                    .contact(new Contact("****", "*****", "*******"))
                    .version("1.0")
                    .build();
        }
    }
    
  6. 在service controller上添加注解

    @Api(tags = "****")
    @RestController
    @RequestMapping("/test")
    public class TestController  {
    
    @ApiOperation(value="****接口")
    @RequestMapping(value = "/print", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public JSONObject print(@Valid TestParam param) throws Exception{
        JSONObject returnJson = new JSONObject();
        returnJson.put("name",param.getName());
        return returnJson;
     }
    
  7. 在service entity上添加注解

    @Data
    @ApiModel
    public class TestParam {
        @ApiModelProperty(name="name",value="姓名",required = true)
        @NotBlank(message = "name不能为空!")
        private String name;
    }
    
  8. 启动zuul、service,访问zuul_ip:port/doc.html,即可查看swaggerUi页面

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值