spring cloud zuul集成swagger2
-
依赖版本
spring cloud <Greenwich.SR1>
spring boot <2.1.3.RELEASE> -
添加依赖(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>
-
zuul/service 启动类添加@EnableSwagger2注解
-
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; } }
-
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(); } }
-
在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; }
-
在service entity上添加注解
@Data @ApiModel public class TestParam { @ApiModelProperty(name="name",value="姓名",required = true) @NotBlank(message = "name不能为空!") private String name; }
-
启动zuul、service,访问zuul_ip:port/doc.html,即可查看swaggerUi页面