Spring Boot 整合Swagger

目录

一、引入依赖

二、自定义配置类

三、写一个测试的controller

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

[五、使用 Swagger 访问接口](#五、使用 Swagger 访问接口)


一、引入依赖

XML 复制代码
     <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>

二、自定义配置类

java 复制代码
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

java 复制代码
/*
 * 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

访问查询商品接口

相关推荐
故事和你918 小时前
洛谷-数据结构1-1-线性表1
开发语言·数据结构·c++·算法·leetcode·动态规划·图论
脱氧核糖核酸__8 小时前
LeetCode热题100——53.最大子数组和(题解+答案+要点)
数据结构·c++·算法·leetcode
脱氧核糖核酸__8 小时前
LeetCode 热题100——42.接雨水(题目+题解+答案)
数据结构·c++·算法·leetcode
一 乐9 小时前
电影院|基于springboot + vue电影院购票管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·电影院购票管理管理系统
恼书:-(空寄9 小时前
JVM GC 日志分析 + 常见 GC 场景 + 实战参数调优
java·jvm
消失的旧时光-19439 小时前
Spring Boot 实战(五):接口工程化升级(统一返回 + 异常处理 + 错误码体系 + 异常流转机制)
java·spring boot·后端·解耦
王老师青少年编程9 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:数列分段 Section I
c++·算法·编程·贪心·csp·信奥赛·线性扫描贪心
王老师青少年编程9 小时前
csp信奥赛C++高频考点专项训练之贪心算法 --【线性扫描贪心】:分糖果
c++·算法·贪心算法·csp·信奥赛·线性扫描贪心·分糖果
_日拱一卒9 小时前
LeetCode:2两数相加
算法·leetcode·职场和发展
py有趣9 小时前
力扣热门100题之零钱兑换
算法·leetcode