Spring Boot 整合Swagger

本文介绍了如何在SpringBoot项目中使用Springfox和Swagger创建API文档,自定义配置类来管理接口路径和元数据,以及如何通过Swagger进行接口测试和可视化展示。
摘要由CSDN通过智能技术生成

目录

一、引入依赖

二、自定义配置类

三、写一个测试的controller

四、正常使用接口测试工具测试

五、使用 Swagger 访问接口


一、引入依赖

     <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

二、自定义配置类

package com.beiyou.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
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;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

 @Bean
    public Docket docket(Environment environment){

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(true)//关闭swagger,默认是true
                .select()
                //RequestHandlerSelectors:配置要扫描的方式,有basePackage("路径")、any():扫描全部,none():全部不扫描
                //RequestHandlerSelectors.withMethodAnnotation():扫描方法上的注解
                //.withClassAnnotation():扫描类上的注解
                .apis(RequestHandlerSelectors.basePackage("com.beiyou"))//指定扫描的包
                .paths(PathSelectors.ant("/**"))//设置请求路径,这里是带有hello的请求路径
                .build() ;
    }

    private ApiInfo apiInfo(){
      //  Contact contact = new Contact("黑米", "https://blog.csdn.net", "xxx@qq.com");
        return new ApiInfo(
                "商品组的Api",
                "请老大检阅",
                "v2.0",
                "https://www.baidu.com",
                null,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }

}

三、写一个测试的controller

/*
 * Copyright (c) 2020, 2024, webrx.cn All rights reserved.
 *
 */
package com.beiyou.controller;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/product")
public class TestController {
    @GetMapping
    public String test() {
        return "查询商品成功";
    }
    @PostMapping
    public String test2() {
        return "添加商品成功";
    }
    @PutMapping
    public String test3() {
        return "修改商品成功";
    }
    @DeleteMapping
    public String test4() {
        return "删除商品成功";
    }
}

四、正常使用接口测试工具测试

四个接口增删改查功能都能实现

五、使用 Swagger 访问接口

使用Swagger访问接口可以有图形化界面展示,更加的方便。

访问的时候具体端口号根据自己的项目而定。

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

 

访问查询商品接口

 

  • 15
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今天的接口写完了吗?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值