springboot整合swagger2,附带源码

swagger2是基于我们在开发前后端分离项目的时候方便后端开发接口的人员做测试,以及团队之间更好的配合。好处我不多说,我在这主要讲怎么运用swagger2。

1.项目准备

/*
 Navicat Premium Data Transfer

 Source Server         : localhost_3306
 Source Server Type    : MySQL
 Source Server Version : 80026
 Source Host           : localhost:3306
 Source Schema         : newsmanagersystem

 Target Server Type    : MySQL
 Target Server Version : 80026
 File Encoding         : 65001

 Date: 20/01/2022 00:05:35
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for topic
-- ----------------------------
DROP TABLE IF EXISTS `topic`;
CREATE TABLE `topic`  (
  `tid` int NOT NULL AUTO_INCREMENT,
  `tname` varchar(50) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`tid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 36 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of topic
-- ----------------------------
INSERT INTO `topic` VALUES (1, '国内');
INSERT INTO `topic` VALUES (2, '国际');
INSERT INTO `topic` VALUES (3, '军事录屏修改');
INSERT INTO `topic` VALUES (4, '体育');
INSERT INTO `topic` VALUES (5, '娱乐');
INSERT INTO `topic` VALUES (6, '社会');
INSERT INTO `topic` VALUES (7, '财经');
INSERT INTO `topic` VALUES (8, '科技');
INSERT INTO `topic` VALUES (9, '健康');
INSERT INTO `topic` VALUES (10, '汽车');
INSERT INTO `topic` VALUES (12, '房产');
INSERT INTO `topic` VALUES (13, '家居');
INSERT INTO `topic` VALUES (14, '旅游');
INSERT INTO `topic` VALUES (15, '文化');
INSERT INTO `topic` VALUES (28, '探索');
INSERT INTO `topic` VALUES (29, '另类');
INSERT INTO `topic` VALUES (31, '测试');
INSERT INTO `topic` VALUES (33, '更新测试');
INSERT INTO `topic` VALUES (34, '录屏测试');

SET FOREIGN_KEY_CHECKS = 1;
/*
 Navicat Premium Data Transfer

 Source Server         : localhost_3306
 Source Server Type    : MySQL
 Source Server Version : 80026
 Source Host           : localhost:3306
 Source Schema         : newsmanagersystem

 Target Server Type    : MySQL
 Target Server Version : 80026
 File Encoding         : 65001

 Date: 20/01/2022 00:05:42
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for news_users
-- ----------------------------
DROP TABLE IF EXISTS `news_users`;
CREATE TABLE `news_users`  (
  `uid` int NOT NULL AUTO_INCREMENT,
  `uname` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `u_pwd` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  PRIMARY KEY (`uid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of news_users
-- ----------------------------
INSERT INTO `news_users` VALUES (1, 'admin', 'admin');
INSERT INTO `news_users` VALUES (2, '王八', '123456');
INSERT INTO `news_users` VALUES (3, '小白', '12345678');
INSERT INTO `news_users` VALUES (4, '哈哈哈', '123456');
INSERT INTO `news_users` VALUES (6, 'WYT', '123456');
INSERT INTO `news_users` VALUES (7, 'DFP', '123456');

SET FOREIGN_KEY_CHECKS = 1;

 准备好我们要的2个数据表

2.引入swagger2的依赖

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

3.使用

这是我们基本的目录结构

 2个控制类

 一个是控制操作用户的请求地址

一个是控制操作类型的请求地址后面我们会用到

在config包下放sw2的配置

package com.example.springbootdata.config;
import com.google.common.base.Predicates;
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.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * Swagger2配置信息
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket UsersApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(UsetApiInfo())
                .select()
                //只显示api路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/User/.*")))
                .build();
    }
    @Bean
    public Docket TopicApiConfig(){
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(TopicApiInfo())
                .select()
                //只显示api路径下的页面
                .paths(Predicates.and(PathSelectors.regex("/Topic/.*")))
                .build();
    }

    private ApiInfo UsetApiInfo(){

        return new ApiInfoBuilder()
                .title("Users测试文档")
                .description("本文档描述了测试接口定义")
                .version("1.0")
                .contact(new Contact("new DFO", "http://33221@qq.com", "33221@qq.com"))
                .build();
    }
    private ApiInfo TopicApiInfo(){

        return new ApiInfoBuilder()
                .title("topic测试文档")
                .description("本文档描述了测试接口定义")
                .version("1.0")
                .contact(new Contact("new DFO", "http://33221@qq.com", "33221@qq.com"))
                .build();
    }
}

 注意要添加的2个注解

前2个方法相当于创建2个管理桌面

后2个方法是向2个桌面输入内容。

.paths(Predicates.and(PathSelectors.regex("/Topic/.*")))

 是指定该页面下管理是Topic下的所有地址

详细代码我在后面会分享这里讲一些关键的地方

我们给controller的方法添加注释方便阅读文档

同样也在类头为整个类添加注释@API

主要的介绍在这里你们可以去测试一下我在这就测试比较常用的3个 

 @Api:修饰整个类,描述Controller的作用

    @ApiOperation:描述一个类的一个方法,或者说一个接口

    @ApiParam:单个参数描述

    @ApiModel:用对象来接收参数

    @ApiModelProperty:用对象接收参数时,描述对象的一个字段

    @ApiImplicitParam:一个请求参数

@ApiImplicitParams:多个请求参数

4.测试

运行项目在浏览器下输入请求我的端口是8080

http://localhost:8080/swagger-ui.html输入这个就可打开页面了

 先点击第一步

这个就是我们刚刚在sw2配置中设置的2个桌面管理 不同的接口

点击第二步

会显示我们controller的请求地址点开具体一个测试

 返回的结果

 有的时候分局传入的数据格式不同sw2会提示你怎么输入数据

 

 

 5.源码分享

data_test: 学习代码分享 (gitee.com)

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现springboot整合swagger2 3.0.0版本,你需要按照以下步骤操作: 1. 创建一个maven项目并引入spring-boot-starter-web和springfox-boot-starter依赖。在pom.xml文件中添加以下代码: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <!-- <version>2.5.6</version> --> <!-- <version>2.6.3</version> --> <!-- <version>2.6.5</version> --> <version>2.7.3</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.26</version> </dependency> ``` 2. 在application.yml配置文件中添加以下内容: ```yaml spring: mvc: pathmatch: matching-strategy: ant_path_matcher ``` 3. 创建启动类,并在其中添加`@EnableSwagger2`注解。例如: ```java @SpringBootApplication @EnableSwagger2 public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } } ``` 这样就完成了springboot整合swagger2 3.0.0版本的配置。你可以根据需要在项目中编写相应的接口文档注解以及其他相关配置。如果需要更详细的操作步骤和示例代码,你可以参考中提供的链接。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Springboot整合Swagger2(3.0.0版本)](https://blog.csdn.net/mo_sss/article/details/130820204)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Springboot整合Swagger UI 3.0.0 版本](https://blog.csdn.net/qq_42102911/article/details/126410050)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值