springboot集成swagger(swagger使用介绍)(汉化教程)

Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。
其中主要要项目如下:

  1. Swagger-tools:提供各种与Swagger进行集成和交互的工具。例如模式检验、Swagger 1.2文档转换成Swagger 2.0文档等功能。

  2. Swagger-core: 用于Java/Scala的的Swagger实现。与JAX-RS(Jersey、Resteasy、CXF…)、Servlets和Play框架进行集成。

  3. Swagger-js: 用于JavaScript的Swagger实现。

  4. Swagger-node-express: Swagger模块,用于node.js的Express web应用框架。

  5. Swagger-ui:一个无依赖的HTML、JS和CSS集合,可以为Swagger兼容API动态生成优雅文档。

  6. Swagger-codegen:一个模板驱动引擎,通过分析用户Swagger资源声明以各种语言生成客户端代码。

接下来就让我们开启集成之路吧!!

  1. 创建springboot 项目
  2. 引入swagger 依赖, 这里需要注意的springboot的版本跟swagger 版本直接的关系,不建议都用最新版本,很有可以会造成项目访问不了swagger UI。
        <!--swagger2-->
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
  1. 创建swagger config类:
@Configuration
public class Swagger2Config {

    @Bean
    public Docket testApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("demo for test")
                .genericModelSubstitutes(DeferredResult.class)
//                .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(true)
                .pathMapping("/")// base,最终调用接口后会和paths拼接在一起
                .select()
                .paths(regex("/api/.*"))//过滤的接口
                .build()
                .apiInfo(testApiInfo());
    }

    @Bean
    public Docket demoApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("demo")
                .genericModelSubstitutes(DeferredResult.class)
//              .genericModelSubstitutes(ResponseEntity.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")
                .select()
                .paths((regex("/demo/.*")))//过滤的接口
                .build()
                .apiInfo(demoApiInfo());
    }

    private ApiInfo testApiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot 集成 Swagger2")//大标题
                .description("Rest api document")//详细描述
                .version("1.0")//版本
                .termsOfServiceUrl("NO terms of service")
                .contact(new Contact("Glen", "https://blog.csdn.net/qq_35623773", "18397641397@163.com"))//作者
                .license("The Apache License, Version 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .build();
    }

    private ApiInfo demoApiInfo() {
        return new ApiInfoBuilder()
                .title("SpringBoot 集成 Swagger2")//大标题
                .description("Rest api document")//详细描述
                .version("1.0")//版本
                .termsOfServiceUrl("NO terms of service")
                .contact(new Contact("Glen", "https://blog.csdn.net/qq_35623773", "18397641397@163.com"))//作者
                .license("The Apache License, Version 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .build();
    }

    @Bean
    public Docket OnlyRestApi() {
        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                Class<?> declaringClass = input.declaringClass();
                // 排除特定类
                if (declaringClass == BasicErrorController.class)
                    return false;
                // 包含特定的类
                if(declaringClass.isAnnotationPresent(RestController.class))
                    return true;
                // 包含特定注解的方法
                if(input.isAnnotatedWith(GetMapping.class) || input.isAnnotatedWith(PostMapping.class))
                    return true;
                return false;
            }
        };
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(restApiInfo())
                .groupName("Only rest api.")
                .useDefaultResponseMessages(false)
                .select()
                .apis(predicate)
                .build();
    }

    private ApiInfo restApiInfo() {
        return new ApiInfoBuilder()
                .title("Only include rest api.")//大标题
                .version("1.0")//版本
                .build();
    }
}

Docket 简介:

public Docket(DocumentationType documentationType) {
    this.apiInfo = ApiInfo.DEFAULT;
    this.groupName = "default";
    this.enabled = true;
    this.genericsNamingStrategy = new DefaultGenericTypeNamingStrategy();
    this.applyDefaultResponseMessages = true;
    this.host = "";
    this.pathMapping = Optional.absent();
    this.apiSelector = ApiSelector.DEFAULT;
    this.enableUrlTemplating = false;
    this.vendorExtensions = Lists.newArrayList();
    this.documentationType = documentationType;
}

从构造方法中我们看出Docket 类包含11个属性,常用属性介绍:

apiInfoAPI 文档相关信息,对应实体类ApiInfoBuilder, 其中包含了API title, description, termsOfServiceUrl, license, version等API 相关信息
groupName分组名
applyDefaultResponseMessages解析response 信息, 默认为true
hosturl 前缀
pathMapping设置api根路径
apiSelector初始化并返回一个API选择构造器 ApiSelectorBuilder类的方法
documentationTypeAPI content-type

ApiSelectorBuilder类的方法:

ApiSelectorBuilder apis(Predicate selector):添加选择条件并返回添加后的ApiSelectorBuilder对象

ApiSelectorBuilder paths(Predicate selector):设置路径筛选,该方法中含一句pathSelector = and(pathSelector, selector);表明条件为相与

RequestHandlerSelectors类的方法:

Predicate any():返回包含所有满足条件的请求处理器的断言,该断言总为true

Predicate none():返回不满足条件的请求处理器的断言,该断言总为false

Predicate basePackage(final String basePackage):返回一个断言(Predicate),该断言包含所有匹配basePackage下所有类的请求路径的请求处理器

PathSelectors类的方法:

Predicate any():满足条件的路径,该断言总为true

Predicate none():不满足条件的路径,该断言总为false

Predicate regex(final String pathRegex):

当我们只需要restful 分格的api时, 我们可以对那些response是url或者vm模板的api进行相应的过滤, 可参考以下方法去实现:

 @Bean
    public Docket OnlyRestApi() {
        Predicate<RequestHandler> predicate = new Predicate<RequestHandler>() {
            @Override
            public boolean apply(RequestHandler input) {
                Class<?> declaringClass = input.declaringClass();
                // 排除特定类
                if (declaringClass == BasicErrorController.class)
                    return false;
                // 包含特定的类
                if(declaringClass.isAnnotationPresent(RestController.class))
                    return true;
                // 包含特定注解的方法
                if(input.isAnnotatedWith(GetMapping.class) || input.isAnnotatedWith(PostMapping.class))
                    return true;
                return false;
            }
        };
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(restApiInfo())
                .groupName("Only rest api.")
                .useDefaultResponseMessages(false)
                .select()
                .apis(predicate)
                .build();
    }

    private ApiInfo restApiInfo() {
        return new ApiInfoBuilder()
                .title("Only include rest api.")//大标题
                .version("1.0")//版本
                .build();
    }
  1. 修改restController
@RestController
@Api("查询公司信息")//用来说明当前 controller 的作用
public class CompanyManagerController {
    private final static Logger logger = LoggerFactory.getLogger(CompanyManagerController.class);
    @Autowired
    private CompanyService companyService;


    @GetMapping("/search/{pageNum}/{pageSize}/company")
    //api 具体描述
    @ApiOperation(value = "根据关键字或者区域查询多个公司", notes = "pageSize 为零时查询所有", tags = {"公司查询"}, httpMethod = "GET")
    //参数描述
    //@ApiImplicitParam(value = "pageNum", defaultValue = "不能为空", required = true)
    //多个参数时
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "pageNum", value = "页数", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "pageSize", value = "页面大小", required = true, dataType = "int"),
            @ApiImplicitParam(paramType="query", name = "keyword", value = "公司名称关键字", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "arCode", value = "区域编号", dataType = "String"),
            @ApiImplicitParam(paramType = "query", name = "arName", value = "区域名字", dataType = "String")
    })
    @ApiResponses({
            @ApiResponse(code = 404, message = "page not found")
    })
    public ResponseEntity<JsonResult> findCompany(@PathVariable("pageNum") int pageNum, @PathVariable("pageSize") int pageSize,
                                                  @RequestParam(value = "keyword", required = false, defaultValue = "") String keyword,
                                                  @RequestParam(value = "arCode", required = false) String arCode,
                                                  @RequestParam(value = "arName", required = false)  String arName){
    logger.info(String.format("get one page company, pageSize: %d, page: %d", pageNum, pageSize));
        JsonResult r = new JsonResult();
        try {
            verifyParames(keyword, arCode, arName);
            List<CMCompany> cmCompanies = companyService.findCompany(pageNum, pageSize, keyword, arCode, arName);
            r.setResult(new CompaniesListResource(cmCompanies));
            r.setStatus("ok");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("error");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }

    private void verifyParames(String keyword, String arCode, String arName) throws Exception {
        if (StringUtils.isEmpty(keyword) && StringUtils.isEmpty(arCode) && StringUtils.isEmpty(arName)) {
            logger.error("keyword, arCode and arName is all null!!!");
            throw new Exception("params error!!");
        }
    }

    @GetMapping("/company/detail")
    //api 具体描述
    @ApiOperation(value = "根据id查询公司具体信息", notes = "查询公司所有信息及送货地址", tags = {"公司查询"}, httpMethod = "GET")
    //参数描述
    @ApiImplicitParam(value = "coid", defaultValue = "不能为空", required = true)
    @ApiResponses({
            @ApiResponse(code = 404, message = "page not found"),
            @ApiResponse(code = 400, message = "内部参数异常")
    })
    public ResponseEntity<JsonResult> findCompanyDetail(@RequestParam(value = "coid") String coid){
        logger.info("findCompanyDetail");
        JsonResult r = new JsonResult();
        try {
            CMCompany cmCompany = companyService.findCompany(coid);
            r.setResult(new CompanyResource(cmCompany));
            r.setStatus("ok");
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus("error");
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }
}

Swagger使用的注解及其说明:

@Api:用在类上,说明该类的作用。

@ApiOperation:注解来给API增加方法说明。

@ApiImplicitParams : 用在方法上包含一组参数说明。

@ApiImplicitParam:用来注解来给方法入参增加说明。

@ApiResponses:用于表示一组响应

@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息

l   code:数字,例如400

l   message:信息,例如"请求参数没填好"

l   response:抛出异常的类   

@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)

l   @ApiModelProperty:描述一个model的属性

注意:@ApiImplicitParam的参数说明:

参数名介绍
paramType:指定参数放在哪个地方
header:请求参数放置于Request Header,使用@RequestHeader获取
query:请求参数放置于请求地址,使用@RequestParam获取
path:(用于restful接口)–>请求参数的获取:@PathVariable
name:参数名
dataType:参数类型
required:参数是否必须传 true
value:说明参数的意思
defaultValue:参数的默认值

5.启动springBoot, 访问: http://localhost:8080/wx/swagger-ui.html#/
在这里插入图片描述

  1. 测试某一个具体的API(输入对应的参数, 点击 try it):
    在这里插入图片描述

到这里,集成就结束了, 附上源码: https://github.com/MrGlen/wxwl
参考: https://blog.csdn.net/sanyaoxu_2/article/details/80555328
官网:https://swagger.io/

swagger 汉化:https://www.jianshu.com/p/7e543f0f0bd8
这里直接使用汉化时有问题的,需要配置静态文件资源路径:

#配置静态资源访问
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath: META-INF/resources
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot中集成Swagger,你需要做以下几个步骤: 1. 首先,确保你使用的是Spring Boot 2.5.x及之前的版本。因为从Spring Boot 2.6.x开始,Swagger已经从Spring Boot中移除了。 2. 在你的Spring Boot应用中添加Swagger的依赖。在pom.xml文件中,添加以下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 3. 在启动类上添加`@EnableSwagger2`注解。这个注解会启用Swagger的功能。你可以将这个注解直接添加到你的Spring Boot启动类上,或者创建一个单独的配置类,在配置类中添加这个注解。 4. 配置Swagger的相关属性。你可以在`application.properties`或`application.yml`文件中添加以下配置: ```yaml springfox.documentation.swagger.v2.path=/swagger springfox.documentation.swagger.ui.enabled=true ``` 这些配置将指定Swagger的路径和UI的启用状态。 5. 编写API文档。在你的控制器类中,使用Swagger的注解来描述你的API接口。例如,你可以使用`@Api`注解来给你的控制器类添加一个API的描述,<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [SpringBoot教程(十六) | SpringBoot集成swagger(全网最全)](https://blog.csdn.net/lsqingfeng/article/details/123678701)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值