springboot项目搭建之Swagger配置

本文档详细介绍了如何使用Swagger2来为SpringBoot项目生成RESTful API文档,并实现接口的在线调试。首先,需要在pom.xml中引入Swagger2的相关依赖。然后,创建SwaggerConfig配置类,通过Docket Bean配置API信息。接下来,在Controller层的接口上添加Swagger注解,如@Api和@ApiOperation等,以描述接口的功能。完成配置后,启动项目,访问http://localhost:8080/swagger-ui.html即可查看和测试接口。此外,还可以使用Apifox进行更专业的接口调试。

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

目录

pom.xml引入依赖

编写配置文件swaggerconfig

在Controller层相应接口加上Swagger注解

最后启动项目在浏览器输入地址即可

补充:在Apifox进行接口调试


pom.xml引入依赖


        <!--        接口文档            -->
        <!-- 引入swagger2的jar包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--引入接口文档页面UI-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

注意:如果后面一系列都配置对了,项目不能运行,可能是Swagger 与 SpringBoot 版本不匹配出现的问题,可以降低SpringBoot 版本。

编写配置文件swaggerconfig


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



@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(true)          // 是否禁用swagger
                .useDefaultResponseMessages(false)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("swagger API文档")
                .description("测试接口文档")
                .version("1.0")
                .build();
    }
}

在Controller层相应接口加上Swagger注解


@RestController
@Api(tags = "角色相关")
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 参数对象
     * @return 单条数据
     * roleId 参数对象
     */
    @ApiOperation("查询单条数据")
    @GetMapping("/id")
    public ResultInfo<Role> selectOne(@RequestParam("id") int id) {
        Role result = roleService.selectById(id);
        if(result != null){
            return ResultInfoUtil.buildSuccess(result);
        }
        return ResultInfoUtil.buildError("查询失败");
    }
}

最后启动项目在浏览器输入地址即可


地址:http://localhost:8080/swagger-ui.html

补充:在Apifox进行接口调试


 配置好后就可以进行接口调试啦

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值