在SpringBoot 中集成Swagger

前提:我的SpringBoot 项目的版本是

java 复制代码
<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.7.18-SNAPSHOT</version>

    <relativePath /> <!-- lookup parent from repository -->

</parent>

一、导入jar包

java 复制代码
<dependency>

    <groupId>io.springfox</groupId>

    <artifactId>springfox-boot-starter</artifactId>

    <version>3.0.0</version>

</dependency>

二、编写SwaggerConfig 配置类

代码:

java 复制代码
package com.hjxjpa.springbootjpa.config;

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.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;


@EnableOpenApi
@Configuration
public class SwaggerConfig {
	
	@Bean
	Docket createRestApi() {
		return new Docket(DocumentationType.OAS_30)
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.hjxjpa.springbootjpa.web"))
				.paths(PathSelectors.any())
				.build();
	}
	
	private ApiInfo apiInfo() {
		System.out.println("apiInfo已经被执行了..................");
		return new ApiInfoBuilder()
				.title("工作平台API ")
				.description("工作平台SwaggerAPI管理")
				.termsOfServiceUrl("htpp://127.0.0.1/")
				.version("1.0")
				.build();
	}

}

三、在controller类中添加注解

代码:

java 复制代码
package com.hjxjpa.springbootjpa.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.hjxjpa.springbootjpa.entity.TempUserEntity;
import com.hjxjpa.springbootjpa.service.TempUserService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "Api-用户管理")
@RestController
@RequestMapping("/api/tempUser")
public class TempUserController {
    @Autowired
    TempUserService tempUserService;
    
    @ApiOperation(value = "用户列表",notes = "用户管理列表")
    @GetMapping("/user")
    public List<TempUserEntity> getUsers(){
        return tempUserService.getUsers();
    }

}

四、配置mvc 路径访问策略

在配置文件中加入以下配置,否则看不到接口内容:

java 复制代码
spring.mvc.pathmatch.matching-strategy=ant-path-matcher 

五、查看效果

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

以上就是swagger集成到springboot项目的基本操作,希望对伙伴们有帮助 !

相关推荐
找不到、了12 分钟前
Java设计模式之<建造者模式>
java·设计模式·建造者模式
Code blocks1 小时前
关于“LoggerFactory is not a Logback LoggerContext but Logback is on ......“的解决方案
java·spring boot·后端
飞翔的佩奇2 小时前
基于SpringBoot+MyBatis+MySQL+VUE实现的经方药食两用服务平台管理系统(附源码+数据库+毕业论文+部署教程+配套软件)
数据库·vue.js·spring boot·mysql·毕业设计·mybatis·经方药食两用平台
04Koi.4 小时前
八股训练--Spring
java·后端·spring
Dcs4 小时前
微软 Copilot 被“越狱”?安全研究员教你一招拿下“沙箱环境 Root 权限”!
java
℡余晖^5 小时前
每日面试题18:基本数据类型和引用数据类型的区别
java
纯洁的小魔鬼5 小时前
Springboot 配置 doris 连接
spring boot·doris·连接池
c_zyer5 小时前
Mermaid流程图可视化系统:基于Spring Boot与Node.js的三层架构实现
spring boot·node.js·流程图·mermaid
hello 早上好5 小时前
消息顺序、消息重复问题
java·中间件
phltxy5 小时前
ArrayList与顺序表
java·算法