在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项目的基本操作,希望对伙伴们有帮助 !

相关推荐
孙浩君2 分钟前
我用 Spring Boot + xterm.js 写了个终端风格互动叙事游戏引擎:游戏即文件,上传一个 JSON 就能玩
java
jose10 分钟前
URL 里多写了一个端口,为什么请求还能调通?
java·后端
孙浩君14 分钟前
我用 Spring Boot 写了个「终端风格模拟经营游戏引擎」:引擎 + JSON 游戏文件,开箱即玩
java
y1su28 分钟前
Leetcode 动态规划 模板
java·数据结构·算法·leetcode·动态规划
Wang's Blog43 分钟前
Java 项目实战: 交友项目 - JWT 概述:为什么要用 Token 换掉 Session
java·ui·交友
SL_staff1 小时前
低代码选型避坑指南:为什么‘能拖拽’不等于‘能交付’?——基于JVS八大引擎的四维交付能力评估框架
java·低代码·spring cloud
李福春2 小时前
技术思考问题6:技术演讲该晒成功架构,还是交代失败与取舍?
java·开发语言·架构·computer use·深圳同盟·腾讯云架构师技术同盟·反演思维
用户7771298509122 小时前
equals和==的区别
java