赞
踩
记录一下 Spring boot 整合增强版的 swagger-ui, 有了这个,基本可以省去postman调用了,极其方便
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- swagger --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swaggwe增强ui --> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>swagger-bootstrap-ui</artifactId> <version>1.9.6</version> </dependency> </dependencies>
## swagger 启用配置
swagger:
enable: true
# 如果为true则禁用所有swagger资源
production: false
package com.zlv11.controller; import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author lvzb * @date 2022/11/15 17:43 **/ @Configuration @EnableSwagger2 @EnableSwaggerBootstrapUI public class Swagger2Config { @Value("${swagger.enable}") Boolean swaggerEnabled; @Bean public Docket createRestApi() { // apis 扫描包范围 return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .enable(swaggerEnabled) .select() .apis(RequestHandlerSelectors.basePackage("com.zlv11")) .paths(PathSelectors.any()) .build(); } /** * API 说明,包含作者、简介、版本、host、服务URL */ private ApiInfo apiInfo() { /** * contact:作者信息 */ return new ApiInfoBuilder() .title("UDS-BACKEND-TITLE 文档") .description("UDS-BACKEND 文档") .version("1.0") .contact(new Contact("吕起凤", "http://www.XXX.com/", "xxx@qq.com")) .build(); } }
/**
* @author lvzb
* @date 2022/11/15 17:37
**/
@Slf4j
@RestController
@Api(tags = "控制层")
@RequestMapping("/swagger")
public class SwaggerTestController {
@PostMapping("/test")
public EchoResponse echo(@RequestBody EchoRequest request) {
return EchoResponse.builder().resp1("Hello world!").build();
}
}
package com.zlv11.controller; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** * @author lvzb * @date 2022/11/15 17:35 **/ @Data @AllArgsConstructor @NoArgsConstructor @Builder @ApiModel(description = "请求入参对象") public class EchoRequest { @ApiModelProperty(value = "请求参数1", required = true) private String re1; }
package com.zlv11.controller; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; /** * @author lvzb * @date 2022/11/15 17:36 **/ @Data @AllArgsConstructor @NoArgsConstructor @Builder @ApiModel(description = "响应对象") public class EchoResponse { @ApiModelProperty(value = "响应字段1") private String resp1; }
http://ip:端口/doc.html
即可看到 增强版的 swagger-ui 文档http://ip:端口/swagger-ui.html
即可看到 原生的 swagger-ui 文档http://localhost:8000/swagger-ui/index.html
即可看到 原生的 swagger-ui 文档(swagger-ui 3.0版本)http://ip:端口/v2/api-docs
即可看到 api的接口 json 内容,可以用于传到 yapi上直接导入生成接口文档,极其方便!Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。