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

访问查询商品接口

相关推荐
q***04054 分钟前
Spring Boot项目中解决跨域问题(四种方式)
spring boot·后端·dubbo
q***64974 分钟前
Spring BOOT 启动参数
java·spring boot·后端
不穿格子的程序员7 分钟前
从零开始写算法——二分-搜索二维矩阵
线性代数·算法·leetcode·矩阵·二分查找
百***78458 分钟前
Java实战:Spring Boot application.yml配置文件详解
java·网络·spring boot
百***628513 分钟前
Spring Boot 3.X:Unable to connect to Redis错误记录
spring boot·redis·后端
你不是我我17 分钟前
【Java 开发日记】SQL 语句左连接右连接内连接如何使用,区别是什么?
java·javascript·数据库
七夜zippoe34 分钟前
Java性能调优工具篇:JMH基准测试与Profiler(JProfiler/Async-Profiler)使用指南
java·开发语言·jprofiler·jmh·async-profiler
從南走到北39 分钟前
JAVA国际版二手车交易二手车市场系统源码支持Android+IOS+H5+APP
android·java·ios
Kuo-Teng1 小时前
LeetCode 19: Remove Nth Node From End of List
java·数据结构·算法·leetcode·链表·职场和发展·list
北i1 小时前
TiDB 关联子查询去关联优化实战案例与原理深度解析
java·数据库·sql·tidb