springBoot(12)---整合Swagger2

随着前后端分离,API接口成为联系前后端的关键。Swagger2作为一款API文档框架,简化了API文档的维护。本文将介绍如何在SpringBoot项目中整合Swagger2,包括配置、接口创建和测试访问步骤。

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

随着互联网技术的发展,一般开发都是前后端分离,那么前端和后端的唯一联系,变成了API接口;API文档变成了前后端开发人员联系的纽带,变得越来越重要,没有API

文档工具之前,大家都是手写API文档的,在什么地方书写的都有,有在confluence上写的,有在对应的项目目录下readme.md上写的,每个公司都有每个公司的玩法,无所谓好

坏。但都有一个很大的诟病就是,如果你的接口改动了,那你必须记得去改你的API文档,而Swagger并不需要。swagger就是一款让你更好的书写API文档的框架。

一、项目搭建

   1、pom.xml

复制代码

        <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、application.yml

复制代码

server:
  port: 8086

#代表当前环境是dev环境
spring:
  profiles:
    active: dev

复制代码

   3、Swagger2DevConfig(配置类)

注意添加@EnableSwagger2注解,这个注解也可以加在springboot启动类上

复制代码

@Configuration
@EnableSwagger2
public class Swagger2DevConfig {

    //默认pro环境
    @Profile({"default", "pro"})
    @Bean
    public Docket createWebApi() {
        return new Docket(DocumentationType.SWAGGER_2).enable(false).select().build();
    }

    //dev环境接口访问地址:http://localhost:8086/swagger-ui.html
    @Profile("dev")
    @Bean
    public Docket createWebApiDev() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfoDev()).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
    }

    //测试环境接口访问地址: test.jincou.com/user/swagger-ui.html
    @Profile("test")
    @Bean
    public Docket createWebApiTest() {
        return new Docket(DocumentationType.SWAGGER_2).host("test.jincou.com/user")
                .protocols(Sets.newHashSet("https")).apiInfo(apiInfoDev()).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfoDev() {
        return new ApiInfoBuilder().title("用户模块API").description(
                "用户api接口文档\n" + "\n" + "测试环境:https://test.jincou.com/user\n").contact(new Contact("张三", "", ""))
                .version("1.0").build();
    }
}

复制代码

      4、PersonController

    这里提供了两个接口

复制代码

@Api(tags = "用户相关接口")
@RestController
@RequestMapping(value = "/web/api/person")
public class PersonController {


    @ApiOperation(value = "用户详细信息", notes = "通过id获得用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", defaultValue = "0")})
    @RequestMapping(value="page",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
    public Person  getById(@RequestParam(value="id",defaultValue = "0") Long id){
        return new Person(id,2,"小小","杭州");
    }

    //大咖分页列表
    @ApiOperation(value = "删除用户信息", notes = "通过ID删除用户信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, paramType = "query", defaultValue = "0")})
    @RequestMapping(value="delete",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE )
    public String  delete(@RequestParam(value="id",defaultValue = "0") Long id){

        return "删除成功";
    }
}

复制代码

      5、Person实体类

复制代码

@ApiModel(description = "用户详细信息")
public class Person {

    @ApiModelProperty(value = "用户Id", position = 1)
    private Long id;

    @ApiModelProperty(value = "用户年龄", position = 2)
    private int age;

    @ApiModelProperty(value = "用户姓名", position = 3)
    private String name;

    @ApiModelProperty(value = "用户所在城市", position = 4)
    private String city;

    public Person(Long id,int age,String name,String city){
        this.id=id;
        this.age=age;
        this.city=city;
        this.name=name;
    }
//提供get和set方法
}

复制代码

     6、本地启动测试

   访问:http://localhost:8086/swagger-ui.html。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值