springboot集成swagger2

本文介绍如何使用Swagger插件自动生成RESTful API文档,简化接口测试流程。通过在pom文件中添加依赖,创建配置类并添加注解,即可实现实时更新的API文档,提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前工作中,对接口测试使用的是Postman或者手写测试网页的形式,每次更新接口,都要手动的更改测试网页的对应内容,如果测试网页已经给了其他同事,同样也要对他进行更新,耗时耗力,这里介绍一种自动生成Restful API文档的插件,可以实时方便的进行接口测试。

1.pom文件添加依赖

<!--集成Swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

2.新建swaggerConfig.java类

package com.wshy.billcheck.config;

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.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author wshy
 * @data 2020/7/22
 **/
@Configuration //在springboot中加载配置文件
@EnableSwagger2 //加载swagger
public class SwaggerConfig {
    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo()) // 调用apiInfo方法
                .pathMapping("/") //配置访问路径
                .select()
                .paths(PathSelectors.regex("/.*")) //匹配路径下的方法
                .build();

    }


    private ApiInfo apiInfo () {
        return new ApiInfoBuilder () //文档说明
                .title ("对账前置系统测试")
                //文档版本说明
                .version ("1.0.0")
                .description ("对账前置支付接口测试")
                .license ("Apache 2.0")
                .build ();
    }
}

3.controller添加注解

在这里插入图片描述

4.启动服务并访问

http://127.0.0.1:9001/swagger-ui.html#/

在这里插入图片描述
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值